File Input / Output

Loading files

Load a text file into a String

val fileContent = File("/path/to/file.txt").readText()

Load a text file into a List<String>

val lines = File("/path/to/file.txt").readLines()

Load binary content into a ByteArray

val bytes = File("/path/to/file.txt").readBytes()

For loading large text files one can use File.bufferedReader().

Saving files

Save a String

File("/path/to/file.txt").writeText("Hello")

Save a ByteArray

File("/path/to/file.txt").writeBytes(myByteArray)

For saving large text files one can use File.bufferedWriter().

JSON

The kotlinx-serialization library makes it possible to convert JSON files to Kotlin objects and back. The library is enabled by default in recent versions of the openrndr-template.

Loading / converting JSON to an object

// An object matching the JSON file.
// Notice how optional properties are nullable.
@Serializable
data class Entry(
    var time: Double,
    var easing: String,
    var rotx: Double?,
    var roty: Double?,
    var x: Double?,
    var y: Double?,
    var scale: Double?,
    var jitter: Double?
)

// from disk
// val json = File("/path/to/a/file.json").readText()

// from a string
val json = """
[
    {
        "time": 0.0,
        "easing": "cubic-in-out",
        "rotx": 0.0,
        "roty": 30.0,
        "x": 0.0,
        "y": 0.0,
        "scale": 1.0,
        "jitter": 0.0
    },
    {
        "time": 1.0,
        "easing": "cubic-in-out",
        "rotx": 0.0,
        "x": 20.0,
        "y": 0.0,
        "scale": 1.0
    },
    {
        "time": 1.3,
        "easing": "cubic-in-out"
    }
]
""".trimIndent()

fun main() = application {
    program {
        val entries = Json.decodeFromString<List<Entry>>(json)

        entries.forEachIndexed { i, entry ->
            println(i)
            println(entry)
        }
    }
}

Saving an object as a JSON file

fun main() = application {
    program {
        val points = List(20) { Vector2.uniform(drawer.bounds) }
        val json = Json.encodeToString(points)
        File("data/points.json").writeText(json)
    }
}

If you want to make the resulting JSON more readable, use prettyPrint:

val pretty = Json { prettyPrint = true }
val json = pretty.encodeToString(points)

CSV

The kotlin-csv library enables loading and saving CSV files. To enable the library:

  1. Find the build.gradle.kts file in your openrndr-template.
  2. Add the line implementation(libs.csv) to your dependencies { ... } block.
  3. Reload Gradle.

Load a CSV file

fun main() = application {
    program {
        // read from `String`
        val csvData = "a,b,c\nd,e,f"
        val rowsA = csvReader().readAll(csvData)

        // read from `java.io.File`
        val file = File("test.csv")
        val rowsB = csvReader().readAll(file)
    }
}

Save a CSV file

fun main() = application {
    program {
        val rows = listOf(listOf("a", "b", "c"), listOf("d", "e", "f"))
        csvWriter().writeAll(rows, "data/test.csv")
    }
}

XML / HTML

The jsoup library enables loading and parsing XML and HTML files. To enable the library:

  1. Find the build.gradle.kts file in your openrndr-template.
  2. Add the line implementation(libs.jsoup) to your dependencies { ... } block.
  3. Reload Gradle.

Get Wikipedia home page and parse news headlines

fun main() = application {
    program {
        val doc = Jsoup.connect("https://en.wikipedia.org/").get()
        println(doc.title())

        val newsHeadlines = doc.select("#mp-itn b a")
        newsHeadlines.forEach {
            println(it.attr("title"))
            println(it.absUrl("href"))
        }
    }
}

edit on GitHub


This site uses Just the Docs, a documentation theme for Jekyll.