這就是我最終做的。它工作的很好,但可能會使用一些微調。我有兩組代碼。一種只是將屬性寫入文件而另一種文件更深入一些。我認爲如果它不存在但是隻有其中一個存在,它們都應該寫入填充。這裏是每個代碼與一些額外的。我現在只是在使用控制檯,而我只是亂搞,所以它根本沒有華麗。創建屬性文件時出現問題
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) {
}
}
它的工作原理,但現在它只是在文件寫入。我嘗試加載它,但我做錯了。我在問題的底部創建了一個新代碼。我需要添加什麼才能讓它起作用。添加到屬性而不是寫完它。 – Funlamb
您更新的代碼有兩個問題。它總是使用新的Properties對象來寫入文件,而不是重新使用在讀取階段加載的文件。這就是爲什麼它總是覆蓋文件。它不會關閉finally塊中的流。有關正確打開/關閉流的示例,請參見http://download.oracle.com/javase/tutorial/essential/io/bytestreams.html。 –
我簡單不明白。我摧毀了我的代碼,看看我需要做什麼,現在它甚至不可識別。我會做更多的研究。 – Funlamb