Java - Read/write Properties from/to .properties file » grokonez

Java - Read/write Properties from/to .properties file » grokonez

In the post, we show how to read/write Properties object from/to Properties File (.properties file). ContentsI. Java – Write Properties object to Properties File1. Properties store() method2. Program – write Properties to FileII. Java – Read Properties File to Properties object1. Properties load() method2. Program – Read from Properties File I. Java – Write Properties … Continue reading "Java – Read/write Properties from/to .properties file"

https://grokonez.com/java/java-read-write-properties-object-properties-file-properties-file

Java – Read/write Properties from/to .properties file

In the post, we show how to read/write Properties object from/to Properties File (.properties file).

I. Java - Write Properties object to Properties File

1. Properties store() method

We use java.util.Properties.store() methods:

// 1.
public void store(OutputStream out,
                      String comments)
                   throws IOException

-> Writes this property list (key and element pairs) in this Properties table 
to the output stream in a format suitable for loading into a Properties table using the load(InputStream) method.

// 2.
public void store(Writer writer,
                    String comments)
                throws IOException

-> Writes this property list (key and element pairs) in this Properties table 
to the output character stream in a format suitable for using the load(Reader) method.

2. Program - write Properties to File


package com.javasampleapproach.propertiesfile;

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

public class JavaWriteProperties2File { public static void main(String[] args) { Properties properties = new Properties(); properties.put("db.username", "username"); properties.put("db.password", "password"); properties.put("db.driver", "org.postgresql.Driver"); properties.put("db.url", "jdbc:postgresql://localhost/testdb");

    /*
     *     Approach 1: 
     *     use -> 'java.util.Properties.store(OutputStream out, String comments) throws IOException'
     */
    String propertiesFile = System.getProperty("user.dir") + "\\file.properties";
    try(OutputStream propertiesFileWriter = new FileOutputStream(propertiesFile)){
        properties.store(propertiesFileWriter, "save to properties file");
    }catch(IOException ioe){
        ioe.printStackTrace();
    }

    /*
     *     Approach 2: 
     *     use -> 'java.util.Properties.store(Writer writer, String comments) throws IOException'
     */
    propertiesFile = System.getProperty("user.dir") + "\\file_1.properties";
    try(Writer propertiesFileWriter = new FileWriter(propertiesFile)){
        properties.store(propertiesFileWriter, "save to properties file");
    }catch(IOException ioe){
        ioe.printStackTrace();
    }
}

}

-> .properties output file:

More at:

https://grokonez.com/java/java-read-write-properties-object-properties-file-properties-file

Java – Read/write Properties from/to .properties file

java properties file

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Java File Class Tutorial | Java.io File Class in Java Example

Java File class represents the path of directories and files. It provides the methods for renaming, deleting, and obtaining the properties of file or directory.

Java Online Training | Java Online Course | ITGuru

Our Java Online Training provide you to learn about Java programming and its different features with realty. Our Java Online Course includes live sessions, live projects

Best Java IDE 2020 | Most Popular Java IDE

Our Java Online Training provide you to learn about Java programming and its different features with realty. Our Java Online Course includes live sessions, live projects

Learning by Doing: How to Learn Java Basics by Building Your Own Project

Check out some ideas for sample projects that can help you learn Java, and programming in general, in a fun and engaging way.