File operations

object FileUtils {

    fun loadResource(resourceName: String, charSet: Charset = Charsets.UTF_8): String {
        val resource: URL? = javaClass::class.java.getResource(resourceName)
        return resource?.readText(charSet) ?: 
            throw IllegalArgumentException("Resource '${resourceName}' not found")
    }

    fun loadFile(fileName: String, charSet: Charset = Charsets.UTF_8): String {
        return File(fileName).bufferedReader(charSet).use { it.readText() }
    }

    fun saveFile(content: String, fileName: String, charSet: Charset = Charsets.UTF_8) {
        File(fileName).bufferedWriter(charSet).use { it.write(content) }
    }
}

Diese kleinen Datei-Utilities benötigt man zwischendurch immer mal wieder.

Advertisements