Skip to content

Instantly share code, notes, and snippets.

@dalmat36
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save dalmat36/65d855a892e892387068 to your computer and use it in GitHub Desktop.

Select an option

Save dalmat36/65d855a892e892387068 to your computer and use it in GitHub Desktop.
Java-IO-Properties
package com.javademo;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class PropertiesDemo {
public static void main(String[] args) {
//Read properties file
Properties p = loadPropertiesFile("props.properties");
//Print out properties
System.out.println("Username: " + p.getProperty("prop.user"));
System.out.println("Password: " +p.getProperty("prop.pass"));
}
public static Properties loadPropertiesFile(String s) {
Properties properties = new Properties();
FileReader input = null;
try {
input = new FileReader(s);
try {
properties.load(input);
} catch (IOException e) {
System.err.println("Failed loading properties: could not read file");
}
} catch (FileNotFoundException e) {
System.err.println("Failed loading properties: file not found");
} finally{
if(input!=null){
try {
input.close();
} catch (IOException e) {
System.err.println("Failed close properties file");
}
}
}
return properties;
}
}
#Simple Properties File
prop.user=username
prop.pass=password
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment