All Downloads are FREE. Search and download functionalities are using the official Maven repository.

name.remal.java.util.Properties.kt Maven / Gradle / Ivy

package name.remal

import java.io.File
import java.io.StringReader
import java.io.StringWriter
import java.util.*
import kotlin.text.Charsets.UTF_8

fun loadProperties(file: File) = Properties().apply { load(file) }
fun loadProperties(content: String) = Properties().apply { load(content) }

fun Properties.load(file: File) {
    file.inputStream().use { load(it) }
}

fun Properties.load(content: String) {
    StringReader(content).use { load(it) }
}

fun Properties.storeAsString(doStripComments: Boolean = true): String {
    var content = StringWriter().use { store(it, ""); it.toString() }
    if (doStripComments) {
        content = content.splitToSequence('\n')
            .map { it.substringBefore('#').trim() }
            .filter(String::isNotEmpty)
            .joinToString("\n")
    }
    return content
}

fun Properties.store(file: File, doStripComments: Boolean = true) {
    file.createParentDirectories().writeText(storeAsString(doStripComments), UTF_8)
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy