/withio

Kotlin minilib for try-with-resources for the input-output case

Primary LanguageKotlin

With IO

A quick idea turned into a Kotlin mini-library for this Java pattern:

try (var input = Files.newBufferedReader(inputFile);
     var output = Files.newBufferedWriter(outputFile))
{
        /* ... */
}

... and with this library ...

import io.mikael.withio.InputOutput

val a = Files.newBufferedReader(inputFile)
val b = Files.newBufferedWriter(outputFile)
InputOutput(a, b).use { io ->
    val data = io.input.readText()
    io.output.write(data)
}
import io.mikael.withio.withInputOutput

withInputOutput<BufferedReader, BufferedWriter>()
    .withInput { Files.newBufferedReader(inputFile) }
    .withOutput { Files.newBufferedWriter(outputFile) }
    .use { io ->
        val data = io.input.readText()
        io.output.write(data)
    }
    .then { io ->
        /* if you still need the input and output for something */
    }