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.

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"
Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as