Scalax IO Output write mode

390 views
Skip to first unread message

Jeshua Bratman

unread,
Oct 19, 2011, 3:35:40 PM10/19/11
to Scala Incubator
This may have an obvious answer, but I can't seem to figure out how to
change the file open mode (e.g. append/truncate etc). I found
StandardOpenOptions, but none of these methods take these options as
parameters.

import scalax.io._
val output : Output = Resource.fromFile("someFile")
output.write("hello")


Thanks for the help,
Jesh

Jesse Eichar

unread,
Oct 20, 2011, 10:47:55 AM10/20/11
to scala-i...@googlegroups.com
Hi,

There are a few options for you.  First of all a resource from file is a Seekable with patch, insert and append options.  That gives you some rough control.

Second if you use the scalax.file API you get full control.  Here is an example.

    import scalax.file.Path

    import scalax.io.StandardOpenOption._

    // open the file for appending,  it will fail if file does not exist and 

    // it will delete the file after editing the file

    Path("someFile").open(Seq(DeleteOnClose)) { out =>

      // any number of Seekable actions can be performed 

      out.append(" an ending")

      out.chars.takeWhile(_ != '.')

    }

    

    Path("someFile").outputStream(WriteAppend:_*).write("appending")

    Path("someFile").outputStream(WriteTruncate:_*).write("replace all data with this")

    Path("someFile").outputStream(Write).write("just replace beginning of file with this")


I am adding this to the collection of samples

Jesse

Jeshua Bratman

unread,
Oct 23, 2011, 11:34:44 PM10/23/11
to Scala Incubator
Great thanks! This helps alot.

--Jesh

On Oct 20, 10:47 am, Jesse Eichar <jesse.eic...@gmail.com> wrote:
> Hi,
>
> There are a few options for you.  First of all a resource from file is a
> Seekable with patch, insert and append options.  That gives you some rough
> control.
>
> Second if you use the scalax.file API you get full control.  Here is an
> example.
>
>     import scalax.file.Path
>
>     import scalax.io.StandardOpenOption._
>
>     // open the file for appending,  it will fail if file does not exist
> and
>
>     // it will delete the file after editing the file
>
>     Path("someFile").open(Seq(DeleteOnClose)) { out =>
>
>       // any number of Seekable actions can be performed
>
>       out.append(" an ending")
>
>       out.chars.takeWhile(_ != '.')
>
>     }
>
>     Path("someFile").outputStream(WriteAppend:_*).write("appending")
>
>     Path("someFile").outputStream(WriteTruncate:_*).write("replace all data
> with this")
>
>     Path("someFile").outputStream(Write).write("just replace beginning of
> file with this")
>
> I am adding this to the collection of samples
>
> Jesse
>
Reply all
Reply to author
Forward
0 new messages