In this short article, you'll learn how to save a string to a text file in Java.

Using Files.writeString() Method

In Java 11, a new static method called writeString() was added to the Files class to easily write a string to a file. Here is how you can use it:

try {
    // write string to a file
    Files.writeString(Path.of("output.txt"), "Hey, there!");

} catch (IOException ex) {
    ex.printStackTrace();
}

To explicitly specify a character encoding, you can do the following:

try {
    // write string to a file
    Files.writeString(Path.of("output.txt"), "Hey, there!",
            StandardCharsets.ISO_8859_1);

} catch (IOException ex) {
    ex.printStackTrace();
}

To create a file if it doesn't already exist or append the string to an existing one, you can pass optional file open options:

try {
    // write string to a file
    Files.writeString(Path.of("output.txt"), "Hey, there!",
            StandardCharsets.ISO_8859_1,
            StandardOpenOption.CREATE,
            StandardOpenOption.APPEND);

} catch (IOException ex) {
    ex.printStackTrace();
}

Using Files.write() Method

For Java 7 or higher, you can use the Files.write() method to write a string to a file as shown below:

try {
    // write string to a file
    Files.write(Paths.get("output.txt"), "Hey, there!".getBytes());

} catch (IOException ex) {
    ex.printStackTrace();
}

To specify a different character encoding other than the default UTF-8, you can do the following:

try {
    // create a string list
    List<String> contents = Collections.singletonList("Hey, there!");
    
    // write string to a file
    Files.write(Paths.get("output.txt"), contents,
            StandardCharsets.UTF_16);

} catch (IOException ex) {
    ex.printStackTrace();
}

To create a non-existing file or append the string to an existing one, use the following code snippet:

try {
    // create a string list
    List<String> contents = Collections.singletonList("Hey, there!");

    // write string to a file
    Files.write(Paths.get("output.txt"), contents,
            StandardCharsets.UTF_16,
            StandardOpenOption.CREATE,
            StandardOpenOption.APPEND);

} catch (IOException ex) {
    ex.printStackTrace();
}

Using BufferedWriter Class

BufferedWriter is another class that you can use to write a string to a text file. Here is an example:

try {
    // create a writer
    BufferedWriter bw = Files.newBufferedWriter(Paths.get("output.txt"));

    // write string to file
    bw.write("Hey, there!");

    // close the writer
    bw.close();

} catch (IOException ex) {
    ex.printStackTrace();
}

Files.newBufferedWriter() also accepts an optional character encoding:

try {
    // create a writer
    BufferedWriter bw = Files.newBufferedWriter(Paths.get("output.txt"),
            StandardCharsets.UTF_8);

    // write string to file
    bw.write("Hey, there!");

    // close the writer
    bw.close();

} catch (IOException ex) {
    ex.printStackTrace();
}

To create a file if it doesn't exist or append the string to an existing one, you can also pass file opening options:

try {
    // create a writer
    BufferedWriter bw = Files.newBufferedWriter(Paths.get("output.txt"),
            StandardCharsets.UTF_8,
            StandardOpenOption.CREATE,
            StandardOpenOption.APPEND);

    // write string to file
    bw.write("Hey, there!");

    // close the writer
    bw.close();

} catch (IOException ex) {
    ex.printStackTrace();
}

Further Reading

You may be interested in other Java I/O articles:

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.