It won't load the properties right! I am using java.util.Properties, but it isn't working! I just want a really simple way to store variables, hich I usually do with properties, but this time it isn't working. Here is the SSCEE (I think thats the right way you name it)
All help is appreciated.import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; public class SSCEE { public static void main(String[] args) throws Exception { Properties prop = new Properties(); File f = new File(System.getProperty("user.home")+"/DogWalking/user-info/user.properties"); FileOutputStream out = null; FileInputStream in = null; if(!f.exists()) { //Creates and sets defaults fine... new File(System.getProperty("user.home")+"/DogWalking/user-info/").mkdirs(); //Create directory f.createNewFile();//Create file in = new FileInputStream(f); out = new FileOutputStream(f); System.out.println("Created a new user.properties! Setting default values"); prop.setProperty("timeWalked", "0"); prop.setProperty("moneyGained", "0"); prop.store(out, "--No Comment--"); out.flush(); out.close(); prop.load(in); in.close(); System.out.println(prop); }else if(f.exists()) { //Finds nothing in = new FileInputStream(f); out = new FileOutputStream(f); System.out.println("Found a user.properties! Getting values!"); prop.load(in); in.close(); if(prop.containsKey("timeWalked") && prop.containsKey("moneyGained")) { System.out.println("Success!"); System.out.println(prop); }else System.out.println("Fail! " + prop); //Every time goes here. prop.store(out, "--No Comment--"); //Just Checking for debugging purposes exactly what it does... and it prints nothing! out.flush(); out.close(); } } }
The first run it prints
And the file looks likeCreated a new user.properties! Setting default values {moneyGained=0, timeWalked=0}
#--No Comment-- #Thu Aug 25 13:50:39 PDT 2011 moneyGained=0 timeWalked=0
The second run (and all following) prints
and the file looks likeFound a user.properties! Getting values! Fail! {}
#--No Comment-- #Thu Aug 25 13:52:43 PDT 2011
Thanks for any help!