- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
file output
Tue, 2009-03-17, 07:11
I just realized that I don't even know how to write to an output file in Scala. I looked in the Odersky book, and I see something on reading from a file, but I cannot find anything on writing to a file. I searched the Scala docs, and I googled it, but amazingly I can't seem to find anything on it. Will someone please give me a clue about how file output is typically done in Scala, or point me to some examples. Thanks.
Russ P.
Russ P.
Tue, 2009-03-17, 08:17
#2
Re: file output
If you're just writing a text file then I'd use java.io.FileWriter instead of the more generic FileOutputStream:
val out = new java.io.FileWriter(fname)
out.write("hello file!")
out.close
Actually Java IO is a complex beast - depending on what you want to do - how performant you want it to be? How large are the files? etc.
If you want to do more complex things than simply writing text to a file or are very worried about performance then I'd suggest you read up about Java IO (or even the Java New IO library for that matter).
At the very least you might want to consider wrapping your FileWriters with a BufferedWriter for text or your FileOutputStreams with BufferedOutputStream for binary files.
Ishaaq
2009/3/17 Christian Szegedy <christian.szegedy@gmail.com>
val out = new java.io.FileWriter(fname)
out.write("hello file!")
out.close
Actually Java IO is a complex beast - depending on what you want to do - how performant you want it to be? How large are the files? etc.
If you want to do more complex things than simply writing text to a file or are very worried about performance then I'd suggest you read up about Java IO (or even the Java New IO library for that matter).
At the very least you might want to consider wrapping your FileWriters with a BufferedWriter for text or your FileOutputStreams with BufferedOutputStream for binary files.
Ishaaq
2009/3/17 Christian Szegedy <christian.szegedy@gmail.com>
I just use the relevant java classes:
var out_file = new java.io.FileOutputStream(fname)
var out_stream = new java.io.PrintStream(out_file)
out_stream.print("hello file!")
out_stream.close
On Mon, Mar 16, 2009 at 11:11 PM, Russ Paielli <russ.paielli@gmail.com> wrote:
> I just realized that I don't even know how to write to an output file in
> Scala. I looked in the Odersky book, and I see something on reading from a
> file, but I cannot find anything on writing to a file. I searched the Scala
> docs, and I googled it, but amazingly I can't seem to find anything on it.
> Will someone please give me a clue about how file output is typically done
> in Scala, or point me to some examples. Thanks.
>
> Russ P.
>
>
I just use the relevant java classes:
var out_file = new java.io.FileOutputStream(fname)
var out_stream = new java.io.PrintStream(out_file)
out_stream.print("hello file!")
out_stream.close
On Mon, Mar 16, 2009 at 11:11 PM, Russ Paielli wrote:
> I just realized that I don't even know how to write to an output file in
> Scala. I looked in the Odersky book, and I see something on reading from a
> file, but I cannot find anything on writing to a file. I searched the Scala
> docs, and I googled it, but amazingly I can't seem to find anything on it.
> Will someone please give me a clue about how file output is typically done
> in Scala, or point me to some examples. Thanks.
>
> Russ P.
>
>