filewriter

How to write text files in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.2, “How to write text files in Scala.”

Problem

You want to write plain text to a file, such as a simple configuration file, text data file, or other plain-text document.

Solution

Scala doesn’t offer any special file writing capability, so fall back and use the Java PrintWriter or FileWriter approaches:

Java “file write” (or “file save”) methods alvin February 1, 2010 - 8:27am

Java write/save FAQ: Can you share an example of how to write to a file in Java?

Sure. Here are two "Java file save" or "Java file write" examples, taken from my Java file utilities article (Java file utilities to write, save, open, read, and copy files). I'm not going to explain these too much, but if you need a simple method to write to a file in your Java programs, I hope these methods will help you.

Java file open, read, and write utilities

Java file utilities FAQ: Do you have any Java file utilities you can share?

As I was working on another Java/Swing application this weekend, I ran across my "Java file utilities" class, and I thought I'd share that class here today. It's nothing too major, but it does include Java methods that let you open, read, write, and copy files using Java.

How to append text to a file with the Java FileWriter class

Java file writing FAQ: How do I append text to the end of a text file in Java?

The short answer is that you should create a FileWriter instance with the append flag set to true, like this:

BufferedWriter bw = new BufferedWriter(new FileWriter("checkbook.dat", true));

The rest of this article explains this.