35

By all accounts, Scala's Source is a bit of a mess - everything I've read about it mentions resources left open, mysterious bugs...

I was wondering whether that was still the case in recent versions of Scala and, if so, what are worthy alternatives?

I've mostly heard of scala-io and scalaz-streams (and, obviously standard Java IO primitives). Did I miss anything? If anyone has experience with these or other projects, what are their respective pros and cons?

I'm inclined to go for scala-io, since I found the author's blog to be a fairly high quality source of useful of information, but I'd love to know more about the alternatives and what other people use.

Nicolas Rinaudo
  • 6,068
  • 28
  • 41
  • Another library worth taking a look at is [scala-arm](https://github.com/jsuereth/scala-arm) – Lundahl Aug 11 '13 at 11:59
  • 3
    My rule of thumb: `Source` for one-off scripts, `java.io` for most stuff, iteratees when I care more about correctness or resource management than performance. I'm [hoping `scalaz-stream`](http://stackoverflow.com/q/18112224/334519) will make it possible to replace all three with one framework (and it [looks promising](http://stackoverflow.com/a/18115189/334519)). – Travis Brown Aug 11 '13 at 15:48
  • 1
    I'm perfectly happy to edit this question and rephrase it in a way that's acceptable to stackoverflow - I just don't see how: the problem I need help with is the lack of a standard Scala IO library, and I'm not sure how to ask about this without violating moderation guidelines. Perhaps there's a better stackexchange site for this question? I'd be happy to take it there if someone could point me in the right direction. – Nicolas Rinaudo Aug 11 '13 at 20:49
  • 4
    @NicolasRinaudo: I personally think this is a perfectly appropriate question here (and have voted to re-open). Adding more detail about your use case(s) might help? – Travis Brown Aug 11 '13 at 20:59
  • 3
    This is a fair question, Scala beautifully abstracts these kinds of problems away from Java - but the topic of I/O (while possible using Java classes) is still waiting for it's proud day in Scala. – LaloInDublin Aug 12 '13 at 17:51
  • My guess is that I/O is at the same time too simple and too diverse for a new library to help much. I/O is about reading and writing bytes, characters and lines, and that's covered by `java.io`. Everything above that layer is application-specific. Stuff below is protocol-specifc (files, http, ...). – jcsahnwaldt Reinstate Monica Aug 14 '13 at 07:00

2 Answers2

6

Rapture IO might be worth trying.

It provides some nice DSL for managing IO resources of various kinds.

Michał Knapik
  • 585
  • 6
  • 20
2

Using the package java.nio.file in Java standard library may also be simple enough if you don't require advance features. For example, to read the lines of a file into memory:

Files.readAllLines(Paths.get("file_name"), StandardCharsets.UTF_8).asScala

And to write a sequence of lines into a file:

val strs = Seq("line1", "line2", "line3")
Files.write(Paths.get("output_file"), strs.mkString("\n").getBytes())

Check http://docs.oracle.com/javase/tutorial/essential/io/file.html for more information.

pishen
  • 319
  • 3
  • 16