10

Does anybody have a guide to this, containing code samples, tips and an outline of the different IO approaches?

I have checked out the the API documentation. There are also some basic examples with scala.io.Source in Programming in Scala.

There are some pretty basic questions already answered here on SO.

I'm looking for something more in depth.

Alternatively, any tips on exactly which bits of the API documentation to focus on and which Java libraries? I'm familiar with System.IO in .NET land, but not so much Java.

Nick A Miller
  • 1,325
  • 1
  • 13
  • 20

5 Answers5

5

Scala's standard library is currently pretty limited for I/O, so you'll probably want to dig into Java's libraries. Oracle's Basic I/O tutorial, covering java.nio, looks like a reasonable starting point.

A while ago there was some discussion about a community driven redesign of Scala I/O called scala-io, but I'm not sure the official status. The mailing list hasn't seen much recent activity, but the code in Github is being actively developed (incubator mailing list and Github project). In his answer, hishadow gave a link to scala-io documentation.

Community
  • 1
  • 1
Kipton Barros
  • 21,002
  • 4
  • 67
  • 80
  • 1
    Let's not forget that you can get Scala for .NET which should be able to use System.IO just like any other .NET application. – Michael Dillon Aug 11 '11 at 04:13
  • @Michael It's being rapidly improved, but do you know if .NET is usable at this point? – Kipton Barros Aug 11 '11 at 15:08
  • @Kipton Barros: It can compile itself, and `scalac` is a pretty large, complex piece of Scala code. The biggest problem is going to be Scala code that doesn't depend purely on Scala libraries, i.e. that has dependencies on Java libraries. – Jörg W Mittag Aug 11 '11 at 15:41
  • @Jörg Getting off topic, but do you know if Scala for .NET will support user defined value types, and VM level reified generics for value types? Those are the aspects of .NET that sound really appealing for numerical work, and a clear advantage over JVM. Scala has @ specialized which helps, but the implementation is still buggy. – Kipton Barros Aug 11 '11 at 16:26
  • @Kipton Barros: It has to be compliant with the Scala Language Specification, which means type erasure for generics. I believe they *did* toy around with the idea but found too many corner cases where code would behave differently. The same applies to structs: Scala doesn't have them. Period. That is, of course, not to say, that a sufficiently clever compiler would not be able to, say, compile purely immutable case classes into structs. – Jörg W Mittag Aug 12 '11 at 20:12
  • @Jörg That's really unfortunate. Structs in particular could make Scala a viable candidate for high performance computing, especially with the coming LLVM backend. Maybe the Scala team will allow a "struct" annotation (as opposed to language change) for immutable case classes on non JVM backends. – Kipton Barros Aug 12 '11 at 23:41
4

My tip is to look also at tools Path,File and Directory.

They have little sugar (that everyone has in their little utils library) like:

val writer = File("/my/file").bufferedWriter
File.closeQuietly(writer)
Directory("myDir").walk

Then have a look at Process like here

Community
  • 1
  • 1
Ido Tamir
  • 3,017
  • 2
  • 19
  • 28
  • 4
    I wouldn't recommend internal classes from the compiler, they can change at will. – soc Aug 11 '11 at 11:15
  • for throwaway scripts - something that I do often, its nice not to need an additional jar. Yes, in real programs its bad. – Ido Tamir Aug 12 '11 at 21:36
4

There is an in-development IO library with documentation available at http://jesseeichar.github.com/scala-io/. Included are examples for different usages.

The source repository is at https://github.com/jesseeichar/scala-io.

hishadow
  • 1,145
  • 7
  • 9
2

I recommend looking at java.nio.

Due to technical limitations of the underlying Java platform implementing IO in Scala wasn't a good idea.

This has changed with Java 7, which provides good Java APIs for everything related to file management.

There are some thoughts about an IO library for Scala, but no decision about that yet.

soc
  • 27,983
  • 20
  • 111
  • 215
1

The author of scala-IO, Jesse Eichar, has recently began a series of articles about using Scala-IO on his blog: here's the 'getting started' entry.

ChucK
  • 2,114
  • 1
  • 18
  • 20