Wednesday, August 8, 2012

Scala-IO Core: Output

The Output object is the primary trait for writing data to a resource. The basic usage is very simple but can get more complex when one wishes to serialize objects.

Lets start with the basic usage: A common need is to write several times to a single Output without overwriting the data. To do this one can use the processing API. A future post(s) will look at the processing API in more detail but for now a simple example:

5 comments:

  1. in.string is not working. Should it be in.slurpString?

    ReplyDelete
    Replies
    1. As I have been writing the daily-scala posts the name slurpString was starting to bother me because it did not conform to the other methods. All the other methods (like byteArray) are dry boring names and slurpString didn't conform to what seemed to be the more consistent naming scheme.

      So in 0.4.1 it is just string to be consistent with bytes, chars, byteArray, etc... Pre 0.4.1 it is slurpString.

      Delete
  2. Why should we use "for" iteration ? not just

    out = output.outputProcessor.headOrTHMS.processor.asOutput
    out.write
    ...
    out.write

    ReplyDelete
  3. Good work! Servers as good lookup for me. One thing, I noticed is that you never close the file after reading/writing. Does it taken care of ? Thanks!

    ReplyDelete
  4. This works for me instead of the for comprehension:
    val o: Output = Resource.fromOutputStream(new FileOutputStream("/path/to/the/file"))
    o.outputProcessor.map(_.write("my name is methos").execute()).execute()

    ReplyDelete