Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Edit (September 2011): since <a href="https://stackoverflow.com/users/608693/eduardo-costa">Eduardo Costa</a> asks about Scala2.9, and since <a href="https://stackoverflow.com/users/204313/rick-777">Rick-777</a> comments that <a href="https://github.com/eengbrec/Scalax.IO/commits/master" rel="nofollow noreferrer">scalax.IO commit history</a> is pretty much non-existent since mid-2009...</p> <p><strong><a href="http://jesseeichar.github.io/scala-io-doc/0.4.3/index.html#!/overview" rel="nofollow noreferrer">Scala-IO</a></strong> has changed place: see its <a href="https://github.com/jesseeichar/scala-io" rel="nofollow noreferrer">GitHub repo</a>, from <a href="https://github.com/jesseeichar" rel="nofollow noreferrer">Jesse Eichar</a> (also <a href="https://stackoverflow.com/users/150272/jesse-eichar">on SO</a>):</p> <blockquote> <p>The Scala IO umbrella project consists of a few sub projects for different aspects and extensions of IO.<br> There are two main components of Scala IO:</p> <ul> <li><strong>Core</strong> - Core primarily deals with Reading and writing data to and from arbitrary sources and sinks. The corner stone traits are <code>Input</code>, <code>Output</code> and <code>Seekable</code> which provide the core API.<br> Other classes of importance are <code>Resource</code>, <code>ReadChars</code> and <code>WriteChars</code>.</li> <li><strong>File</strong> - File is a <code>File</code> (called <code>Path</code>) API that is based on a combination of Java 7 NIO filesystem and SBT PathFinder APIs.<br> <code>Path</code> and <code>FileSystem</code> are the main entry points into the Scala IO File API.</li> </ul> </blockquote> <pre><code>import scalax.io._ val output:Output = Resource.fromFile("someFile") // Note: each write will open a new connection to file and // each write is executed at the begining of the file, // so in this case the last write will be the contents of the file. // See Seekable for append and patching files // Also See openOutput for performing several writes with a single connection output.writeIntsAsBytes(1,2,3) output.write("hello")(Codec.UTF8) output.writeStrings(List("hello","world")," ")(Codec.UTF8) </code></pre> <hr> <p>Original answer (January 2011), with the old place for scala-io:</p> <p>If you don't want to wait for Scala2.9, you can use the <a href="https://github.com/scala-incubator/scala-io" rel="nofollow noreferrer">scala-incubator / scala-io</a> library.<br> (as mentioned in "<a href="https://stackoverflow.com/questions/1762843/why-doesnt-scala-source-close-the-underlying-inputstream">Why doesn't Scala Source close the underlying InputStream?</a>")</p> <p>See <a href="https://github.com/scala-incubator/scala-io/blob/master/scala-io/src/samples/samples.scala" rel="nofollow noreferrer">the samples</a></p> <pre><code>{ // several examples of writing data import scalax.io.{ FileOps, Path, Codec, OpenOption} // the codec must be defined either as a parameter of ops methods or as an implicit implicit val codec = scalax.io.Codec.UTF8 val file: FileOps = Path ("file") // write bytes // By default the file write will replace // an existing file with the new data file.write (Array (1,2,3) map ( _.toByte)) // another option for write is openOptions which allows the caller // to specify in detail how the write should take place // the openOptions parameter takes a collections of OpenOptions objects // which are filesystem specific in general but the standard options // are defined in the OpenOption object // in addition to the definition common collections are also defined // WriteAppend for example is a List(Create, Append, Write) file.write (List (1,2,3) map (_.toByte)) // write a string to the file file.write("Hello my dear file") // with all options (these are the default options explicitely declared) file.write("Hello my dear file")(codec = Codec.UTF8) // Convert several strings to the file // same options apply as for write file.writeStrings( "It costs" :: "one" :: "dollar" :: Nil) // Now all options file.writeStrings("It costs" :: "one" :: "dollar" :: Nil, separator="||\n||")(codec = Codec.UTF8) } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload