Problem creating a properties file
This is what I finally ended up doing. It works great but could probably use some fine tuning.
File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties");
FileInputStream inStream = null;
FileOutputStream outStream = null;
Properties config = new Properties();
try{
if (file.exists()){//Checks if it exists.
inStream = new FileInputStream(file);
if (inStream.available() >= 0){//Cheacks if it has anything in it.
config.load(inStream);
System.out.println(config);
}
}
config.setProperty(property , score);//New property
outStream = new FileOutputStream(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("You just pooped the pants");
} finally{//Closes both input and output Streams if they are open
try {
if(inStream != null)
inStream.close();
if (outStream != null)
outStream.close();
} catch (IOException e) {
}
}
}
I have two sets of code. One that just writes a property to a file and one that is a bit more in depth. I think they should both be writing the fill if it doesn't exist but only one of them does. Here is each code with some extras. I'm just using the console right now and I'm just messing around so its not flashy at all.
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.");
}
}
And here is the one that just writes a property without adding any thing to it. This method is the one that creates the file but I feel that "File file = new File" should do it for both. But then again feelings don't account for much when programming. I'd love an explanation. Thanks.
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);
}
}
Redo on the code with your help:
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.");
}
It throws the IOException but it does write the file. How would I close it in a finally block? I haven't used those yet. How do you just load the file first? I'm still working on it I just wanted to get this to you while you were still on. Thanks for your help.
I'm still not getting this very well. I have put in notes of what I think everything is doing. I'll be asking my friend later tonight what is going on but he is more of a MatLab guy.
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开发者_StackOverflow社区 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) {
}
}
A File object represents a file path. Creating a File instance doesn't create a file on the file system. Opening an FileOutputStream and writing to it is the operation which creates the file on the file system.
Your first code snippet tries to read and write from/to a file at the same time. You should read from the file, close the input stream, and then open an output stream, write and close the output stream. Reading from a file if it doesn't exist will throw an IOException, so you have to handle this possibility. And any input/output stream should always be closed in a finally block.
The code throws an IOException because config.load() reads from the file. But since you are handling the exception, the code does not fail at that point and continues to write to the file. If you first write to the file and then read from it, you would not get the exception. You could load the file first, if the file had data on it.
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();
The above code reads only if there is data in the stream and so it will not throw an exception
精彩评论