aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/com/romangraef/jrconfig/FilePropertiesProvider.kt
blob: fda7bf9067a9d388955c17f36bdbe84b373d0613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.romangraef.jrconfig

import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.util.*

class FilePropertiesProvider(private val file: File) : ConfigProvider {
    private var properties: Properties = Properties()

    init {
        try {
            properties.load(FileInputStream(file))
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    companion object {
        @JvmStatic
        fun create(fileName: String): ConfigProvider {
            return FilePropertiesProvider(File(fileName))
        }
    }

    override fun provideData(point: String): String? {
        return properties.getProperty(point)
    }

    override fun setData(point: String, data: String) {
        properties.setProperty(point, data)
        save()
    }

    private fun save() {
        try {
            properties.store(FileOutputStream(file), "application config")
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }


}