Java – Read/write Properties from/to .properties file
Java
215
properties
5
Male avatar

loveprogramming viết ngày 22/04/2021

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

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

545 bài viết.
97 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 10 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 9 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 9 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://ozenero.com/kotlinpropertiesreadwritepropertiesfilepropertiesxmlfile Kotlin Properties – Read/Write Properties from/to .properties/.XML Fi...
loveprogramming viết 5 tháng trước
0 0
Male avatar
0 0
https://grokonez.com/kotlin/kotlinpropertiesreadwritepropertiesfilepropertiesxmlfile Kotlin Properties – Read/Write Properties from/to .properties...
loveprogramming viết 4 tháng trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
545 bài viết.
97 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!