Java properties file (with extention .properties) is normally used to store the configurable parameters or strings for Internationalization and localization. In the file, each parameter is stored as a key/value pair of strings.
The post will demo how to read / write the properties file.
Read from a properties file
Here is the sample properties file content:
1 2 3 4 5 6 |
# properties username=john password=tina875 role=normal user |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package net.tecbar.file.properties; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.Properties; public class ReadPropertiesDemo { public static void main(String[] args) { InputStream input = null; try { input = new FileInputStream("/opt/setting.properties"); Properties prop = new Properties(); prop.load(input); // get the property value by key System.out.println(prop.getProperty("username")); System.out.println(prop.getProperty("password")); System.out.println(prop.getProperty("role")); System.out.println("==== list all key/value pair ===="); // show name/value in loop Enumeration<?> e = prop.propertyNames(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); String value = prop.getProperty(key); System.out.println("name : " + key + ", Value : " + value); } } catch(FileNotFoundException ex) { ex.printStackTrace(); // add you logic to handle exception } catch(IOException e) { e.printStackTrace(); // add you logic to handle exception } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); // add you logic to handle exception } } } } } |
Output:
1 2 3 4 5 6 7 8 9 |
john tina875 normal user ==== list all key/value pair ==== name : password, Value : tina875 name : username, Value : john name : role, Value : normal user |
If the file and class is in the same package, you can use
1 2 3 4 5 6 |
Properties prop = new Properties(); InputStream in = getClass().getResourceAsStream("setting.properties"); prop.load(in); in.close(); |
or if they are in different packages
1 2 3 4 |
InputStream in = getClass().getResourceAsStream("/net/tecbar/mypackage/foo.properties"); |
Write a properties file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package net.tecbar.file.properties; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Properties; public class WritePropertiesDemo { public static void main(String[] args) { OutputStream output = null; try { output = new FileOutputStream("/opt/config.properties"); // set the properties value final Properties prop = new Properties(); prop.setProperty("username", "jsmith"); prop.setProperty("password", "tk90Yda"); prop.setProperty("email", "jsmith@techbar.net"); prop.store(output, null); } catch(IOException ex) { ex.printStackTrace(); // add you logic to handle exception } finally { if (output != null) { try { output.close(); } catch (IOException e) { e.printStackTrace(); } } } } } |
Generated content
1 2 3 4 5 6 |
#Thu Apr 16 17:17:04 PDT 2015 email=jsmith@techbar.net password=tk90Yda username=jsmith |