Create text file in Java with contents

Creating a text file (with given contents) is pretty straightforward task in Java. Recent Java versions and popular libraries like Apache Commons or Guava make this task even easier than it used to be. Below are some ways how to do it.

PrintWriter

The task can be achieved using PrintWriter class. At first, a new instance of PrintWriter class is created which in turn creates a new file in the file system. Then subsequent calls to methods print(), printf(), println(), append(), format() or write() populate the contents of the file. Method printf() is especially useful because it provides formatting functionality.
At the end, method close() must be called (either explicitly, or by try-with-resources block) to properly close and save the file.

try (PrintWriter writer = new PrintWriter("/tmp/aa.txt",
StandardCharsets.UTF_8)) {
writer.println("Line one");
writer.printf("Line %d\n", 2);
writer.println("Line three");
}

FileWriter

It is also possible to utilize lower level class FileWriter. The behaviour is very similar but misses some convenient methods.

try (FileWriter writer = new FileWriter("/tmp/bb.txt", StandardCharsets.UTF_8)) {
writer.write("Line one\n");
writer.write("Line 2\n");
writer.write("Line three");
}

Files class since Java 7

If two above methods are still too complicated, I have a great news. It is possible to create a new file with defined contents using single call to Files.write() method.

List lines = new ArrayList<>();
lines.add("Line one");
lines.add("Line 2");
Files.write(Paths.get("/tmp/cc.txt"), lines);

Alternatively, a call to Files.writeString() can be used to create a file from String.

FileUtils from Apache Commons IO

Apache Commons IO library provides its own class FileUtils with plenty of file-related functionality. Method writeLines can be used for our purpose:

List lines = new ArrayList<>();
lines.add("Line one");
lines.add("Line 2");
FileUtils.writeLines(new File("/tmp/dd.txt"), "UTF-8", lines);

Alternatively, a call to FileUtils.writeStringToFile() can be used to create a file from String.

Files class from Guava

Also Google Guava library provides its own class Files to serve the same purpose:

List lines = new ArrayList<>();
lines.add("Line one");
lines.add("Line second");
CharSink charSink = Files.asCharSink(new File("/tmp/ee.txt"), StandardCharsets.UTF_8);
charSink.writeLines(lines);

Here the code is slightly more complicated. Alternatively, a call to Fils.write() can be used to create a file from String.

About Robert Piasecki

Husband and father, Java software developer, Linux and open-source fan.
This entry was posted in Guava, Java and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.