Basic Read Write

These examples are a quick introduction to performing basic IO using the Scala IO API

Using Codecs

When converting bytes to and from characters a Codec is needed for the encoding and decoding. Unlike Java Scala IO does not have a default it requires that the Codec be declared. However, to simplify the declaration most methods have implicit codec parameters so the Codec only needs to be declared once. The following examples show reading characters from input streams. Writing is follows the same pattern
    import scalax.io._
    import java.io._

    val in: InputStreamResource[FileInputStream] = Resource.fromInputStream(new FileInputStream("file"))

    // declare the Codec explicitly
    val string:String = in.slurpString(Codec.UTF8)
    val chars: ResourceView[Char] = in.chars(Codec("UTF8"))

    // create a ReadChars so that Codec only needs to be specified once
    val readChars: ReaderResource[Reader] = in.reader(Codec.ISO8859)
    val string2:String = readChars.slurpString
    val chars2: ResourceView[Char] = readChars.chars

    // Finally you can delcare an implicit val once and all calls will implicitly use that codec
    implicit val codec = Codec.UTF8

    val string3:String = in.slurpString
    val chars3: ResourceView[Char] = in.chars
    val readChars2: ReaderResource[Reader] = in.reader