Write Properties - Java File Path IO

Java examples for File Path IO:Property Files

Description

Write Properties

Demo Code


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

public class WriteProperties {

    public final static void main(String[] args) {
        Properties prop = new Properties();
        OutputStream output = null;

        try {/* ww w. jav a2 s .c  o m*/

            output = new FileOutputStream("config.properties");

            // set the properties value
            prop.setProperty("database", "localhost");
            prop.setProperty("dbuser", "user name");
            prop.setProperty("dbpassword", "password");

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

        } catch (IOException io) {
            io.printStackTrace();
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

}

Related Tutorials