How to save a string to a text file using Java

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.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.