devxlogo

Treat the User to Consistency Between Sessions

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, elegantmechanism for storage and management of properties. An extension ofjava.util.Hashtable, the Properties class represents a persistent set ofproperty pairs that can be written to, and read from a stream. Eachproperty pair consists of a key and a value. Both key and value are ofString 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 thefollowing 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 1999HEIGHT=100WIDTH=200TEXT_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 1999HEIGHT=300WIDTH=300*/        //Now load themdefProp = 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 notfound.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 withthe getProperties method.

 "public void store(OutputStream out,String header) throws IOException"

This method writes this property list (key and element pairs) in thisProperties table to the output stream in a format suitable for loading intoa Properties table using the load method.

See also  Why ChatGPT Is So Important Today

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"
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist