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