Set Properties in Text with Java

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

Set properties in text with Java.

PropertyManager.java

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

public class PropertyManager
{
	public static void setProperties (List<String[]> values)
	{
		Properties properties = new Properties();
		try
		{
			for (String[] value : values)
			{
				properties.setProperty(value[0], value[1]);
			}
			properties.store(new FileOutputStream("config.properties"), null);
		}
		catch (IOException ioe)
		{
			
		}
	}
}

Test.java

import java.util.ArrayList;
import java.util.List;

public class Test
{
	public static void main (String[] args)
	{
		List<String[]> properties = new ArrayList<String[]>();
		properties.add(new String[]{ "PROPERTY_1", "This is property 1."});
		properties.add(new String[]{ "PROPERTY_2", "This is property 2."});
		PropertyManager.setProperties(properties);
	}
}

Related Posts

Tagged with:  

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
	}
}

Related Posts

Tagged with:  

Read Properties from XML with Java

On April 26, 2013, in Java, Programming, by LuCuS

Read properties from XML using Java.

properties.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <entry key="PROPERTY_1">This is property 1.</entry>
    <entry key="PROPERTY_2">This is property 2.</entry>
</properties>

PropertyManager.java

import java.io.InputStream;
import java.util.Properties;

public class PropertyManager
{
	public static String getProperty (String propertyName)
	{
		Properties props = new Properties();
		InputStream is = PropertyManager.class.getClassLoader().getResourceAsStream("properties.xml");
		try
		{
			props.loadFromXML(is);
			return props.getProperty(propertyName);
		}
		catch (Exception e)
		{
			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
	}
}

Related Posts

Tagged with:  

Convert String to Stream with Java

On March 29, 2013, in Java, Programming, by LuCuS

Convert string to stream with Java.

	public static InputStream convertStringToStream(String input)
	{
		InputStream is = new ByteArrayInputStream(input.getBytes());
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		return is;
	}

Related Posts

Tagged with:  

Convert Stream to String with Java

On March 29, 2013, in Java, Programming, by LuCuS

Convert stream to string with Java.

	public static String convertStreamToString(InputStream is)
	{
	    Scanner s = new Scanner(is).useDelimiter("\\A");
	    String output = s.hasNext() ? s.next() : "";
	    s.close();
	    return output;
	}

Related Posts

Tagged with: