Read Properties from Text with Java

On May 3, 2013, in Java, Programming, by LuCuS

Read properties from text with Java.

config.properties

PROPERTY_1=This is property 1.
PROPERTY_2=This is property 2.

PropertyManager.java

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

public class PropertyManager
{
	public static String getProperty (String propertyName)
	{
		Properties properties = new Properties();
		try
		{
			// retrieve properties file from filesystem
			properties.load(new FileInputStream("config.properties"));
			
			// retrieve properties file from class path
			//properties.load(PropertyManager.class.getClassLoader().getResourceAsStream("config.properties"));
			return properties.getProperty(propertyName);
		}
		catch (IOException ioe)
		{
			return null;
		}
	}
}

Test.java

public class Test
{
	public static void main (String[] args) throws Exception
	{
		String property1 = PropertyManager.getProperty("PROPERTY_1");
		String property2 = PropertyManager.getProperty("PROPERTY_2");
		// DO SOMETHING WITH PROPERTIES
	}
}

Thank you for your interest in my site. If you find the information provided on this site useful, please consider making a donation to help continue development!

PayPal will open in a new tab.
$2.00
$5.00
Other

Related Posts

Tagged with:  

Leave a Reply