Problem With Properties (+SSCEE)
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)
Code Java:
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();
}
}
}
All help is appreciated.
The first run it prints
Code :
Created a new user.properties! Setting default values
{moneyGained=0, timeWalked=0}
And the file looks like
Code :
#--No Comment--
#Thu Aug 25 13:50:39 PDT 2011
moneyGained=0
timeWalked=0
The second run (and all following) prints
Code :
Found a user.properties! Getting values!
Fail! {}
and the file looks like
Code :
#--No Comment--
#Thu Aug 25 13:52:43 PDT 2011
Thanks for any help!
Re: Problem With Properties (+SSCEE)
With a few mods most of your code works:
Code :
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
public class PropertiesProblem
{
final static String PropFN = "PropertiesProblem.ini"; //<<<<<<<<<
public static void main(String[] args) throws Exception
{
Properties prop = new Properties();
File f = new File(PropFN); //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();
in = new FileInputStream(f);
prop.load(in);
in.close();
System.out.println("created=" + prop); // {moneyGained=0, timeWalked=0}
/*
Created a new user.properties! Setting default values
created={moneyGained=0, timeWalked=0}
*/
}else if(f.exists())
{ //Finds nothing
in = new FileInputStream(f);
// out = new FileOutputStream(f); // WHat does this do???
System.out.println("Found a user.properties! Getting values!");
prop.load(in);
System.out.println("read=" + prop);
in.close();
if(prop.containsKey("timeWalked") && prop.containsKey("moneyGained"))
{
System.out.println("Success!");
}else
System.out.println("Fail! " + prop); //Every time goes here.
out = new FileOutputStream(f); // WHat does this do???
prop.store(out, "--No Comment--"); //Just Checking for debugging purposes exactly what it does... and it prints nothing!
out.flush();
out.close();
/*
Found a user.properties! Getting values!
read={moneyGained=0, timeWalked=0}
Success!
*/
}
}
}