2011-07-01 42 views
0

這就是我最終做的。它工作的很好,但可能會使用一些微調。我有兩組代碼。一種只是將屬性寫入文件而另一種文件更深入一些。我認爲如果它不存在但是隻有其中一個存在,它們都應該寫入填充。這裏是每個代碼與一些額外的。我現在只是在使用控制檯,而我只是亂搞,所以它根本沒有華麗。創建屬性文件時出現問題

private void properties() { 
    System.out.println("What would you like to name the .properties file?"); 
    sTest.stringReader(); 
    String fileName =sTest.getString();// This will be the name of the file. 
    System.out.println("What property would you like to change?"); 
    sTest.stringReader(); 
    String property= sTest.getString(); 
    System.out.println("What would you like to change the " + property + " to?"); 
    sTest.stringReader(); 
    String score = sTest.getString(); 

     try { 
      File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties"); 
      FileInputStream inStream = new FileInputStream(file); 
      Properties config = new Properties(); 
      config.load(inStream); 
      // Create a new property 
      config.setProperty(property , score); 
      FileOutputStream outStream = new FileOutputStream(file); 
      config.store(outStream, "Property"); 
      inStream.close(); 
      outStream.close(); 
      config.list(System.out); 
     } catch (IOException ioe){ 
      System.out.println("Chould not write file."); 
     } 
} 

這裏是一個只寫屬性而不添加任何東西的地方。這種方法是創建文件的方法,但我覺得「文件文件=新文件」應該爲兩者做。但是當編程時再次感受不到很多。我很樂意解釋。謝謝。

private void myWrite() { 
    System.out.println("What would you like to name your file?"); 
    sTest.stringReader(); 
    String fileName =sTest.getString();// This will be the name of the file. 
    System.out.println("What would you like to put in you file?"); 
    sTest.stringReader(); 
    String letsWrite = sTest.getString(); 
     try { 
      File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties"); 
      FileOutputStream fileStream = new FileOutputStream(file); 
      write(fileStream, letsWrite); 
     } catch (IOException ioe){ 
      System.out.println("Could not write file" + ioe); 
     } 
    } 

重做對您的幫助代碼:

System.out.println("What would you like to change the " + property + " to?"); 
    sTest.stringReader(); 
    String score = sTest.getString(); 

    try { 
     File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties"); 
     FileInputStream inStream = new FileInputStream(file); 
     Properties config = new Properties(); 
     config.load(inStream); 
     inStream.close(); 
    } catch (IOException ioe){ 
     System.out.println("Chould not read file."); 
    } 

    try{ 
     File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties"); 
     FileOutputStream outStream = new FileOutputStream(file); 
     Properties config = new Properties(); 
     config.setProperty(property , score); 
     config.store(outStream, "Property"); 
     outStream.close(); 
    } catch (IOException ioe){ 
     System.out.println("Chould not write file."); 
    } 

它拋出IOException異常,但它確實寫入文件。我將如何關閉它在一個finally塊?我還沒有使用過。你怎麼剛剛加載文件?我仍在努力,我只是想在你還在的時候把這個給你。謝謝你的幫助。

我還沒有得到很好。我已經記下了我認爲一切都在做的事情。今晚晚些時候我會問我的朋友怎麼回事,但他更像是一個MatLab傢伙。

File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties");//new instance of these file 
    FileInputStream inStream = null; //Creates variable out side my try block 
    FileOutputStream outStream = null;//Creates variable out side my try block 
    Properties config = new Properties();//Creates variable out side my try block 
    try { 
     inStream = new FileInputStream(file); //Loads the file to the Input Stream 
     config.load(inStream); //Loads the config with what was in the inStream. 

    } catch (IOException ioe){//Handles any problems 
     System.out.println("Chould not read file."); 
    } 

    try{ 
     //inStream = new FileInputStream(file);//Do I need to do this again? 
     //config.load(inStream);// Do I need to do this again? 

     //Creates a new property 
     config.setProperty(property , score); 

     outStream = new FileOutputStream(file); // Preps the outPut stream to write to the file 
     config.store(outStream, "Property");//Names the Properties that are in the file Property 
     config.list(System.out);//Prints out all the properties. 
    } catch (IOException ioe){//Handles any problems 
     System.out.println("Chould not write file."); 
    } finally{//Closes both input and output Streams 
     try { 
      inStream.close();//It says these need to be in a try/catch block also. 
     } catch (IOException e) { 
     } 
     try { 
      outStream.close(); 
     } catch (IOException e) { 
     } 
    } 

回答

2

File對象表示文件路徑。創建文件實例不會在文件系統上創建文件。打開FileOutputStream並寫入它是在文件系統上創建文件的操作。

你的第一個代碼片段試圖同時讀寫文件。您應該從文件中讀取數據,關閉輸入流,然後打開輸出流,寫入並關閉輸出流。如果文件不存在,從文件讀取將拋出IOException,因此您必須處理這種可能性。並且任何輸入/輸出流應始終在finally塊中關閉。

+0

它的工作原理,但現在它只是在文件寫入。我嘗試加載它,但我做錯了。我在問題的底部創建了一個新代碼。我需要添加什麼才能讓它起作用。添加到屬性而不是寫完它。 – Funlamb

+0

您更新的代碼有兩個問題。它總是使用新的Properties對象來寫入文件,而不是重新使用在讀取階段加載的文件。這就是爲什麼它總是覆蓋文件。它不會關閉finally塊中的流。有關正確打開/關閉流的示例,請參見http://download.oracle.com/javase/tutorial/essential/io/bytestreams.html。 –

+0

我簡單不明白。我摧毀了我的代碼,看看我需要做什麼,現在它甚至不可識別。我會做更多的研究。 – Funlamb

1

代碼拋出IOException,因爲config.load()從文件讀取。但是,由於您正在處理異常,因此代碼在此時不會失敗並繼續寫入文件。如果您先寫入文件然後再讀取它,則不會得到異常。如果文件上有數據,可以先加載文件。

File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties"); 
    FileInputStream inStream = new FileInputStream(file); 
    if(inStream.available() > 0){ 
     Properties config = new Properties(); 
     config.load(inStream); 
    } 
    inStream.close(); 

上面的代碼只讀取如果在流中的數據,所以它不會拋出異常

+0

這不是代碼失敗的原因。原因在於他在第一個Properties實例中加載文件,然後在寫入之前用一個新實例替換Properties實例。 –

+0

它爲什麼要拋出異常?雖然它很危險,但您可以在同一個文件中同時打開輸入和輸出流。你能指出你的案件的任何文件?那在Google上失敗了 – Jiraiya