Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problem With Properties (+SSCEE)

  1. #1
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default 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)
    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
    Created a new user.properties! Setting default values
    {moneyGained=0, timeWalked=0}
    And the file looks like
    #--No Comment--
    #Thu Aug 25 13:50:39 PDT 2011
    moneyGained=0
    timeWalked=0

    The second run (and all following) prints
    Found a user.properties! Getting values!
    Fail! {}
    and the file looks like
    #--No Comment--
    #Thu Aug 25 13:52:43 PDT 2011

    Thanks for any help!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem With Properties (+SSCEE)

    With a few mods most of your code works:
    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!
     
             */
     
    		}
    	}
    }

  3. The Following User Says Thank You to Norm For This Useful Post:

    Tjstretch (August 25th, 2011)

Similar Threads

  1. [SOLVED] Accessing && Editing properties of objects in an array. Plus a few more questions.
    By CameronFaust in forum Collections and Generics
    Replies: 31
    Last Post: August 10th, 2011, 07:35 PM
  2. Reload .properties
    By chinna in forum Java Theory & Questions
    Replies: 3
    Last Post: July 14th, 2011, 01:32 AM
  3. Store HexDecimal Color Code in .properties file?
    By techwiz24 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 27th, 2011, 08:45 PM
  4. Structured Properties File?
    By moka in forum Java Theory & Questions
    Replies: 7
    Last Post: October 19th, 2010, 11:14 AM
  5. Accessing Properties File
    By java_mein in forum Java Servlet
    Replies: 5
    Last Post: May 14th, 2010, 02:44 AM