Wednesday, January 13, 2016

Read a property file

1/12/2016


Source: http://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/

.properties is a file extension for files mainly used in Java related technologies to store the configurable parameters of an application. They can also be used for storing strings for Internationalization and localization; these are known as Property Resource Bundles.
Each parameter is stored as a pair of strings, one storing the name of the parameter (called the key/map), and the other storing the value.

source: http://www.avajava.com/tutorials/lessons/how-do-i-read-a-properties-file.html

How do I read a properties file?
Author: Deron Eriksson
Description: This Java tutorial describes how to read properties from a file.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1


Page:    1 2 >

The JavaSW Properties class offers a very convenient method of storing data for your application. It consists of various String values and String keys that you use to look up these values. It is basically a hashtable. The load method of the Properties class offers a very convenient mechanism for loading key/value String pairs from a properties file in the file system.
Let's create a ReadPropertiesFile class. This class reads in properties from a 'test.properties' file at the root level of our project.
'testing' project
The ReadPropertiesFile class creates an input stream from the test.properties file. It creates a Properties object and loads the properties (key/value pairs) from the input stream. It then enumerates over the keys of the Properties object, and retrieves the value for each key. The key/value pairs are displayed via standard output.

ReadPropertiesFile.java

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

public class ReadPropertiesFile {
 public static void main(String[] args) {
  try {
   File file = new File("test.properties");
   FileInputStream fileInput = new FileInputStream(file);
   Properties properties = new Properties();
   properties.load(fileInput);
   fileInput.close();

   Enumeration enuKeys = properties.keys();
   while (enuKeys.hasMoreElements()) {
    String key = (String) enuKeys.nextElement();
    String value = properties.getProperty(key);
    System.out.println(key + ": " + value);
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}
The contents of the test.properties file are displayed below. Each key is represented on the left side of the = sign, and each value is on the right side of the = sign. Comments are designated by lines starting with # signs.

test.properties

# This is my test.properties file
favoriteFood=kun pao chicken
favoriteVegetable=artichoke
favoriteSoda=Dr Pepper

The ReadPropertiesFile class is executed below. We can see the results of the execution at the bottom of the screen capture.
Execution of ReadPropertiesFile
In the console, the three key/value pairs are displayed. Notice that they are not displayed in the order that they are entered in the properties file. This is because the Properties object is a hashtable, which is a data structure that is optimal for quick storage and retrieval of individual data values, but it does not really have a sense of order to the returned results.

1 comment:

  1. To easily translate strings, you could try a software localization management platform like https://poeditor.com/. It can simplify the workflow a lot.

    ReplyDelete