Skip to content

Instantly share code, notes, and snippets.

@alghanmi
Created November 20, 2013 09:04
Show Gist options
  • Select an option

  • Save alghanmi/7560028 to your computer and use it in GitHub Desktop.

Select an option

Save alghanmi/7560028 to your computer and use it in GitHub Desktop.
Java Properties Files
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertiesDemo {
PropertiesDemo() {
}
//Print Properties
public void showProperties(Properties props) {
for(String key : props.stringPropertyNames()) {
String val = props.getProperty(key);
System.out.println("Key: '" + key + "' val: '" + val + "'");
}
}
public static void main(String args[]) {
PropertiesDemo pd = new PropertiesDemo();
Properties props = new Properties();
try {
FileInputStream in = new FileInputStream("propFile.properties");
props.load(in);
in.close();
} catch(IOException e) {
e.printStackTrace();
} catch(IllegalArgumentException iae) {
iae.printStackTrace();
}
pd.showProperties(props);
}
}
#Sample Key-Value pairs
a:propA
b:propB
c:propC
#Add multi lines and space
d : another Property
e : multi\nline\nproperty
#Also, use equal sign instead of colon
f = fProp
g = gProp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment