Kotlin Properties – Read/Write Properties from/to .properties/.XML File
kotin
3
properties
5
XML
7
Male avatar

loveprogramming viết ngày 07/05/2021

https://grokonez.com/kotlin/kotlin-properties-read-write-properties-file-properties-xml-file

Kotlin Properties – Read/Write Properties from/to .properties/.XML File

In the post, we show how to Read/Write Properties from/to .Properties/.XML files by Kotlin language.

I. Kotlin - Read/Write Properties from/to .Properties file

1. Write Properties to .Properties file

1.1 Properties store() method

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


// 1.
fun store(out: OutputStream, comments: String): Unit

-> 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.
fun store(writer: Writer, comments: String): Unit

-> 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.

1.2 Kotlin Program – write Properties to .properties file


package com.javasampleapproach.kotlin.properties

import java.io.FileOutputStream
import java.io.FileWriter
import java.io.IOException
import java.util.Properties

fun main(args: Array) {
    val properties = 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")

    var propertiesFile = System.getProperty("user.dir") + "\\file.properties"
        
    /*
     *  Approach 1: 
     *  use -> 'java.util.Properties.store(out: OutputStream, comments: String)'
     */
    var fileOutputStream = FileOutputStream(propertiesFile)
    properties.store(fileOutputStream, "save to properties file")
    
    /*
     *  Approach 2: 
     *  use -> 'java.util.Properties.store(writer: Writer, comments: String)'
     */
    propertiesFile = System.getProperty("user.dir") + "\\file_1.properties"
    val fileWriter = FileWriter(propertiesFile)
    properties.store(fileWriter, "save to properties file")
}

-> .properties output file:

More at:

https://grokonez.com/kotlin/kotlin-properties-read-write-properties-file-properties-xml-file

Kotlin Properties – Read/Write Properties from/to .properties/.XML 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 9 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 7 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 7 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/android/kotlinfirebaseauthenticationhowtosignupsigninsignoutverifyemailandroid Kotlin Firebase Authentication – How to Sign U...
loveprogramming viết 2 tháng trước
0 0
Male avatar
0 0
https://grokonez.com/springframework/springboot/kotlinspringboot/kotlinspringbootuploaddownloadfilemultipartfilethymeleafbootstrap4 Kotlin SpringB...
loveprogramming viết 3 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á!