Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Beginner
Oct 5, 1999

Treat the User to Consistency Between Sessions

It is common for an application to retain user preferences between sessions. In Java, the java.util.Properties class provides you with a simple, elegant mechanism for storage and management of properties. An extension of java.util.Hashtable, the Properties class represents a persistent set of property pairs that can be written to, and read from a stream. Each property pair consists of a key and a value. Both key and value are of String data types.

You should note that java.util.Properties has a protected data member "protected Properties defaults". This is for a default set of properties. but since you cannot directly set a protected data member, you can extend java.util.Properties like the following manner:

 

import java.util.*;

public class Prop1 extends java.util.Properties
{
    public Prop1()
    {
        super();
    }
    public void setDefault(Properties defProp)
    {
        defaults = defProp;
    }    
}
Now, let's do some saving, and loading of properties:
 
java.util.Properties defProp = new java.util.Properties();
defProp.put("HEIGHT", "100");
defProp.put("WIDTH", "200");
defProp.put("TEXT_COLOR", "BLACK");		
String propFileName = "defProp.prp";
String comment = "DEFAULT APP SETTINGS, //
date will be different for you";
java.io.File outFile = new java.io.File(propFileName);
java.io.OutputStream out = null;
try
{
    out = new java.io.FileOutputStream(outFile);
    defProp.save(out, comment);
}
catch(java.io.IOException ioe){/*handle exception*/}
finally
{
    try{out.flush();out.close();}
   catch(Exception e){/*handle exception*/}
   out = null;		        
}
/*
now, if you look at defProp.prp in your app directory, you see:
#DEFAULT APP SETTINGS, //the date will be different for you 
of course
#Fri Sep 24 09:55:02 EDT 1999
HEIGHT=100
WIDTH=200
TEXT_COLOR=BLACK
*/
Prop1 prop = new Prop1();
prop.setDefault((java.util.Properties) defProp);
prop.put("HEIGHT","300");
prop.put("WIDTH","300");
propFileName = "prop.prp";
comment = "APP SETTINGS, //date will be different for you 
of course";
outFile = new java.io.File(propFileName);
try
{
     out = new java.io.FileOutputStream(outFile);
     prop.save(out, comment);
}
catch(java.io.IOException ioe){/*handle exception*/}
finally
{
    try{out.flush();out.close();}
    catch(Exception e){/*handle exception*/}
    out = null;		        
}
/*
now, if you look at prop.prp in your app directory, 
you see        
#APP SETTINGS, //the date will be different for you 
of course
#Fri Sep 24 09:51:24 EDT 1999
HEIGHT=300
WIDTH=300
*/        
//Now load them
defProp = null;
prop = null;
defProp = new java.util.Properties();
propFileName = "defProp.prp";
java.io.File inFile = new java.io.File(propFileName);
java.io.FileInputStream in = null;
try
{
    in = new java.io.FileInputStream(inFile);
    defProp.load(in);
}
catch(java.io.IOException ioe){/*handle exception*/}
finally
{
    try{in.close();}
    catch(Exception e){/*handle exception*/}
    in = null;		        
}
prop = new Prop1();
propFileName = "prop.prp";
inFile=null;
inFile = new java.io.File(propFileName);
in = null;
try
{
    in = new java.io.FileInputStream(inFile);
    prop.load(in);
}
catch(java.io.IOException ioe){/*handle exception*/}
finally
{
    try{in.close();}
    catch(Exception e){/*handle exception*/}
     in = null;		        
}
prop.setDefault(defProp);        
System.out.println(prop.getProperty("HEIGHT"));
System.out.println(prop.getProperty("WIDTH"));
System.out.println(prop.getProperty("TEXT_COLOR"));//
this from default
//there is also a 
//"public String getProperty(String key,String defaultValue)
//method that returns the default value argument if the 
property is not
found.
System.out.println(prop.getProperty("BACKGROUND", "WHITE"));
Pay attention to the version of JDK you use; Java 2 has added some methods to java.util.Properties:
 
"public Object setProperty(String key,String value)"
This method calls the hashtable method put. Provided for parallelism with the getProperties method.
 
"public void store(OutputStream out,String header) throws 
IOException"
This method writes this property list (key and element pairs) in this Properties table to the output stream in a format suitable for loading into a Properties table using the load method.

Java 2 has deprecated the following:

 
"public void save(OutputStream out,String header)"
in favor of
 
"public void store(OutputStream out,String header) throws 
IOException"
Behrouz Fallahi
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date