The properties object contains key and value pair both as a string. The java.util.Properties class is the subclass of Hashtable.
It can be used to get property value based on the property key. The Properties class provides methods to get data from properties file and store data into properties file. Moreover, it can be used to get properties of system.
Related Article: Using the Static Keyword in Java
Advantage of properties file
Recompilation is not required, if information is changed from properties file: If any information is changed from
Loading properties
To load a properties file bundled with your application:
public class Defaults { public static Properties loadDefaults() { try (InputStream bundledResource = Defaults.class.getResourceAsStream("defaults.properties")) { Properties defaults = new Properties(); defaults.load(bundledResource); return defaults; } catch (IOException e) { // Since the resource is bundled with the application, // we should never get here. throw new UncheckedIOException( "defaults.properties not properly packaged" +" with application", e); } } }
Saving Properties as XML
Storing Properties in a XML File
The way you store properties files as XML files is very similar to the way you would store them as .properties files. Just instead of using the store() you would use storeToXML().
public void saveProperties(String location) throws IOException{ // make new instance of properties Properties prop = new Properties(); // set the property values prop.setProperty("name", "Steve"); prop.setProperty("color", "green"); prop.setProperty("age", "23"); // check to see if the file already exists File file = new File(location); if (!file.exists()){ file.createNewFile(); } // save the properties prop.storeToXML(new FileOutputStream(file), "testing properties with xml"); }
When you open the file it will look like this.
Loading Properties from a XML File
Now to load this file as a properties you need to call the loadFromXML() instead of the load() that you would use with regular .properties files.
public static void loadProperties(String location) throws FileNotFoundException, IOException{ // make new properties instance to load the file into Properties prop = new Properties(); // check to make sure the file exists File file = new File(location); if (file.exists()){ // load the file prop.loadFromXML(new FileInputStream(file)); // print out all the properties for (String name : prop.stringPropertyNames()){ System.out.println(name + "=" + prop.getProperty(name)); } } else { System.err.println("Error: No file found at: " + location); } }
When you run this code you will get the following in the console:
age=23
color=green
name=Steve
Property files caveat: trailing whitespace
Take a close look at these two property files which are seemingly completely identical:
except they are really not identical:
Since trailing whitespace is preserved the value of lastName would be “Smith” in the first case and “Smith ” in the second case.
Very rarely this is what users expect and one and can only speculate why this is the default behavior of Properties class. It is however easy to create an enhanced version of Properties that fixes this problem. The following class, TrimmedProperties, does just that. It is a drop-in replacement for standard Properties class.
Since trailing whitespace is preserved the value of lastName would be “Smith” in the first case and “Smith ” in the second case.
Very rarely this is what users expect and one and can only speculate why this is the default behavior of Properties class. It is however easy to create an enhanced version of Properties that fixes this problem. The following class, TrimmedProperties, does just that. It is a drop-in replacement for standard Properties class.
import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.util.Map.Entry; import java.util.Properties; /** * Properties class where values are trimmed for trailing whitespace if the * properties are loaded from a file. * * <p> * In the standard {@link java.util.Properties Properties} class trailing * whitespace is always preserved. When loading properties from a file such * trailing whitespace is almost always unintentional. This class fixes * this problem. The trimming of trailing whitespace only takes place if the * source of input is a file and only where the input is line oriented (meaning * that for example loading from XML file is not changed by this class). * For this reason this class is almost in all cases a safe drop-in replacement * for the standard Properties * class. * * <p> * Whitespace is defined here as any of space (U+0020) or tab (U+0009). ** */ public class TrimmedProperties extends Properties { /** * Reads a property list (key and element pairs) from the input byte stream. * * <p>Behaves exactly as {@link java.util.Properties#load(java.io.InputStream) } * with the exception that trailing whitespace is trimmed from property values * if inStream is an instance of FileInputStream. * * @see java.util.Properties#load(java.io.InputStream) * @param inStream the input stream. * @throws IOException if an error occurred when reading from the input stream. @Override public void load(InputStream inStream) throws IOException { if (inStream instanceof FileInputStream) { // First read into temporary props using the standard way Properties tempProps = new Properties(); tempProps.load(inStream); // Now trim and put into target trimAndLoad(tempProps); } else { super.load(inStream); } } /** * Reads a property list (key and element pairs) from the input character stream in a simple line-oriented format. * * <p>Behaves exactly as {@link java.util.Properties#load(java.io.Reader)} * with the exception that trailing whitespace is trimmed on property values * if reader is an instance of FileReader. * * @see java.util.Properties#load(java.io.Reader) } * @param reader the input character stream. * @throws IOException if an error occurred when reading from the input stream. * / @Override public void load(Reader reader) throws IOException { if (reader instanceof FileReader) { // First read into temporary props using the standard way Properties tempProps = new Properties(); tempProps.load(reader); // Now trim and put into target trimAndLoad(tempProps); } else { super.load(reader); } } private void trimAndLoad(Properties p) { for (Entry entry : p.entrySet()) { if (entry.getValue() instanceof String) { put(entry.getKey(), trimTrailing((String) entry.getValue())); } else { put(entry.getKey(), entry.getValue()); } } } /** * Trims trailing space or tabs from a string. * * @param str * @return * / public static String trimTrailing(String str) { if (str != null) { // read str from tail until char is no longer whitespace for (int i = str.length() - 1; i >= 0; i--) { if ((str.charAt(i) != ' ') && (str.charAt(i) != '\t')) { return str.substring(0, i + 1); } } } return str; } }