How to read and write properties files in Java

Summary

In Java, properties files are used to store data and configuration. This tutorial will show you how to store and read properties files in Java 8.

Using InputStream and OutputStream

Write to properties file

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

public class PropertiesFileSample {

final private static String PROPERTIES_FILE_PATH="config.properties";

public static void main(String[] args) {

Properties properties = new Properties();

// using java 8 try-with-resources structure,
// so the output stream will be closed automatically
try(OutputStream outputStream = new FileOutputStream(PROPERTIES_FILE_PATH)){

// set some values
properties.setProperty("username", "sampleusername");
properties.setProperty("url", "tutoref.com");

// store the values
properties.store(outputStream, null);

} catch (IOException e) {
e.printStackTrace();
}
}

}

The result will look like this :

#Sat Jul 08 23:55:53 EDT 2017
url=tutoref.com
username=sampleusername

Read from a properties file

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

public class PropertiesFileSample {

final private static String PROPERTIES_FILE_PATH="config.properties";

public static void main(String[] args) {

Properties properties = new Properties();

// using java 8 try-with-resources structure
// so the input stream will be closed automatically
try(InputStream inputStream = new FileInputStream(PROPERTIES_FILE_PATH)){

// read a value
String url = properties.getProperty("url");

// display the value
System.out.println(url);

} catch (IOException e) {
e.printStackTrace();
}
}
}

Load properties from classpath

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

public class PropertiesFileSample {

final private static String PROPERTIES_FILE_PATH="config.properties";

public static void main(String[] args) {

Properties properties = new Properties();

// using java 8 try-with-resources structure
// so the input stream will be closed automatically
try(
InputStream inputStream=
PropertiesFileSample.class.getResourceAsStream(PROPERTIES_FILE_PATH)
){

// load the properties
properties.load(inputStream);

// read a value
String url = properties.getProperty("url");

// display the value
System.out.println(url);

} catch (IOException e) {
e.printStackTrace();
}
}
}

If you want to load the properties file from a non-static method, you should use the following:

InputStream is = getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE_PATH);

Leave a Reply

Your email address will not be published. Required fields are marked *