Tuesday, June 3, 2008

How to store an array in a Java properties file

Below describes how to store a two-dimensional array in a properties file.

Delineate the items in the array by commas and semicolons.

The properties file would be like:


myProperty=name1,age1,job1,location1;name2,age2,job2,location2;

So each person is divided by a semicolin, but the attribute of that person by a comma. The same code works with any amount of attributes per person.

Split up the property into a String[][] variable type:
    //get array split up by the semicolin
String[] a = propFile.getProperty(propertyName).split(";");

//create the two dimensional array with correct size
String[][] array = new String[a.length][a.length];

//combine the arrays split by semicolin and comma
for(int i = 0;i < a.length;i++) {
array[i] = a[i].split(",");
}

Now you have a two dimensional array from a property file!

Below is a complete example:


Properties File
#delineated by Name,Capital,Nickname;Name,Capital,Nickname; etc....
stateInfo=California,Sacramento,The Golden State;Pennsylvania,Philadelphia,Keystone State;Texas,Austin,Lone Star State

Java Code
package myProject;

import java.io.FileInputStream;
import java.util.Properties;

public class Main {

//Key for 2nd part of array
final static int NAME = 0;
final static int CAPITAL = 1;
final static int NICKNAME = 2;

public static void main(String[] args) throws Exception {

//get properties file
Properties prop = new Properties();
prop.load(new FileInputStream("C:\\myStatesPropertiesFile.txt"));

//get two dimensional array from the properties file that has been delineated
String[][] stateInfos = fetchArrayFromPropFile("stateInfo",prop);


//below code will print out all the states, their capitals, and nicknames
for (int i = 0; i < stateInfos.length; i++) {
System.out.print("State "+ i + ":");
System.out.print("\n");
System.out.print("Name: " + stateInfos[i][NAME]);
System.out.print("\n");
System.out.print("Capital: " + stateInfos[i][CAPITAL]);
System.out.print("\n");
System.out.print("NickName: " + stateInfos[i][NICKNAME]);
System.out.print("\n");

}
}

/**
* Creates two dimensional array from delineated string in properties file
* @param propertyName name of the property as in the file
* @param propFile the instance of the Properties file that has the property
* @return two dimensional array
*/

private static String[][] fetchArrayFromPropFile(String propertyName, Properties propFile) {

//get array split up by the semicolin
String[] a = propFile.getProperty(propertyName).split(";");

//create the two dimensional array with correct size
String[][] array = new String[a.length][a.length];

//combine the arrays split by semicolin and comma
for(int i = 0;i < a.length;i++) {
array[i] = a[i].split(",");
}
return array;
}
}

4 comments:

Anonymous said...

# chart colors
colors.pie = #FF0000, #00FF00, #0000FF

You don't have to split the value manually, you can retrieve an array or a java.util.List directly with:

String[] colors = config.getStringArray("colors.pie");
List colorList = config.getList("colors.pie");

Alternatively, you can specify a list of values in your properties file by using the same key on several lines:

# chart colors
colors.pie = #FF0000;
colors.pie = #00FF00;
colors.pie = #0000FF;

All of the features related to list handling described for AbstractConfiguration also apply to properties files, including changing the list delimiter or disabling list handling at all.

Tobias W. said...

Anonymous is wrong.

He/she is referring to features only available in Apache Commons libraries. Sun's Java libraries don't offer anything like getStringArray() on resource bundle implementations.

See also:

http://java.sun.com/javase/6/docs/api/java/util/Properties.html

http://commons.apache.org/configuration/userguide/howto_basicfeatures.html

Anonymous said...

Harrisburg is the capital of Pennsylvania, not Philadelphia

Anonymous said...

Dumb code.

Say a.length is 15, because you have 15 2-element entries.

String[][] array = new String[a.length][a.length];

creates 15 15-entry arrays, then inside the loop:

array[i] = a[i].split(",");

throws away each 15-element empty array in favor of a new 2-element one.

Better to make the first line:

String [][] array = new String[15][];

No need to fill-in the second dimension as you're planing on inserting those in the loop.