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:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.