Provide Best Programming Tutorials

Java-Properties file examples

The properties file is used to store the properties. Developers can read and write properties from or into it.

This post will show:

  • How to write into the write properties file
  • How to read from the properties file

How to write properties into properties file

Let’s say we want to write database properties into properties file and the file looks like this:

package propertiesfiledemo;

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

public class WritePropertiesToPropertiesFile {
    public static void main(String[] args) {
        OutputStream outputStream  = null;

        try {
            outputStream = new FileOutputStream("db.properties");

            Properties prop = new Properties();

            // set the properties value
            prop.setProperty("db.url", "www.andrew-programming.com");
            prop.setProperty("db.user", "andrew_programming_");
            prop.setProperty("db.password", "andrew_programming_password");

            // save properties to project root folder
            prop.store(outputStream, null);

            System.out.println(prop);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Output

{db.password=andrew_programming_password, db.user=andrew_programming_, db.url=www.andrew-programming.com}

A file named db.properties file will be created which has content below.

#Wed Oct 02 22:23:45 CST 2019
db.password=andrew_programming_password
db.user=andrew_programming_
db.url=www.andrew-programming.com

How to read properties from the properties file

We will read the properties from the properties file we just created

Properties file

db.user=andrew_programming_username
db.password=andrew_programming_password
db.url=localhost

code

package propertiesfiledemo;

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

public class ReadPropertiesFromPropertiesFile {

    public static void main(String[] args) {
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("db.properties");
            Properties properties = new Properties();
            properties.load(inputStream);

            System.out.println(properties.getProperty("db.user"));
            System.out.println(properties.getProperty("db.password"));
            System.out.println(properties.getProperty("db.url"));


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
andrew_programming_
andrew_programming_password
www.andrew-programming.com

Leave a Reply

Close Menu