Basic Read Write

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

Basic Output

Basic output options
    import scalax.io._
    import scalax.io.Resource
    import java.io.{
      ByteArrayOutputStream,FileOutputStream,
      PrintStream, OutputStreamWriter
    }

    // Note: The file API is nearly finished allowing one to write directly to files without the
    // cumbersome new FileOutputStream shown below.  This is the "pure" Scala IO solution
    Resource.fromOutputStream(new FileOutputStream("scala.html")) write "data".getBytes()
    Resource.fromOutputStream(new FileOutputStream("scala.html")) write Array[Byte](1,2,3)

    // strings and bytes can both be written to Output objects but strings need a Codec
    // for encoding the strings.  As usual the codec can be explicit or implicitly declared
    Resource.fromOutputStream(new ByteArrayOutputStream()).write("howdy")(Codec.UTF8)

    implicit val defaultCodec: Codec = Codec.UTF8

    // An Output object cannot be created from a Writer as a writer may have an unknown codec that is
    // used for encoding the strings and without knowing which coded is being used an Output object
    // cannot be created so a WriteChars object is created
    // WriteChars have the benefit of not needing to have a codec declared since the underlying writer
    // takes care of encoding
    Resource.fromWriter(new OutputStreamWriter(new ByteArrayOutputStream())).writeString("howdy")


    Resource.fromOutputStream(new PrintStream(new ByteArrayOutputStream())).writer.writeString("howdy")

    // Channels can also be wrapped in Resource objects and accessed as normal Input/Output objects
    val resource = Resource.fromWritableByteChannel(new FileOutputStream("file").getChannel)