If you're simply outputting text, rather than any binary data, the following will work:

PrintWriter out = new PrintWriter("filename.txt");

Then, write your String to it, just like you would to any output stream:

out.println(text);

You'll need exception handling, as ever. Be sure to close the output stream when you've finished writing.

out.close()

If you are using Java 7 or later, you can use the "try-with-resources statement" which will automatically close your PrintStream when you are done with it (ie exit the block) like so:

try (PrintWriter out = new PrintWriter("filename.txt")) {
    out.println(text);
}

You will still need to explicitly throw the java.io.FileNotFoundException as before.

Answer from Jeremy Smyth on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › files-class-writestring-method-in-java-with-examples
Files Class writeString() Method in Java with Examples - GeeksforGeeks
July 23, 2025 - // Implementation of writeString() method in Java import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; public class GFG { public static void main(String[] args) { // Initializing file Path with some conditions Path filePath = Paths.get("/home/mayur/", "temp", "gfg.txt"); try { // Write content to file Files.writeString(filePath, "Hello from GFG !!", StandardOpenOption.APPEND); // Verify file content String content = Files.readString(filePath); System.out.println(content); } catch (IOException e) { e.printStackTrace(); } } }
🌐
Baeldung
baeldung.com › home › java › java io › java – write to file
Java - Write to File | Baeldung
December 1, 2023 - Note how we’re not only writing a raw String to a file, but also some formatted text with the printf method. We can create the writer using FileWriter, BufferedWriter, or even System.out.
🌐
HowToDoInJava
howtodoinjava.com › home › java 11 › java 11 files.writestring(): writing text to a file
Java 11 Files.writeString(): Writing Text to a File
October 12, 2023 - The java.nio.file.Files class has two overloaded static methods to write content to file. public static Path writeString​(Path path, CharSequence csq, OpenOption... options) throws IOException public static Path writeString​(Path path, CharSequence csq, Charset cs, OpenOption... options) throws IOException · The first method writes all content to a file, using the UTF-8 charset. The first method is equivalent to writeString(path, string, StandardCharsets.UTF_8, options).
🌐
Medium
medium.com › @AlexanderObregon › javas-files-write-method-explained-288e75dfc721
Java’s Files.write() Method Explained | Medium
March 20, 2025 - In this example, Files.write() writes a list of strings to a file called example.txt using the UTF-8 charset. If the file doesn't exist, it will be created automatically. This example showcases the simplicity and power of Files.write(), which reduces the code complexity traditionally associated with file I/O in Java.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-save-a-string-to-a-file
Java Program to Save a String to a File - GeeksforGeeks
July 23, 2025 - PrintWriter class is used to write the string data on file using the write() method. ... Create an instance of the file. Create a stream of PrintWriter and pass the file instance into it.
🌐
CodeGym
codegym.cc › java blog › java io & nio › java – write to file
Java – Write to File
December 26, 2024 - Finally, we print a success message to the console if the write operation is successful or an error message if an exception is caught. The writeString() method is a convenient way to write a String to a file using Java 11 or later.
🌐
W3Schools
w3schools.com › java › java_files_write.asp
Java Write To Files
Explanation: This program tries to write some text into a file named filename.txt. If everything works, the program will print "Successfully wrote to the file." in the console. If something goes wrong (for example, the file cannot be opened), it will print "An error occurred." instead. Since Java 7, you can use try-with-resources.
Find elsewhere
🌐
Stack Abuse
stackabuse.com › java-save-write-string-into-a-file
Java: Save/Write String Into a File
August 4, 2020 - Since Java 11, the Files class contains a useful utility method Files.writeString(). This method comes in two variants. The most basic form requires a Path of the file to write to and the textual contents. The other variant also accepts an optional CharSet: Path path = Paths.get("output.txt"); ...
🌐
Mkyong
mkyong.com › home › java › java create and write to a file
Java create and write to a file - Mkyong.com
October 2, 2020 - In Java, we can use Files.write to create and write to a file. String content = "..."; Path path = Paths.get("/home/mkyong/test.txt"); // string -> bytes Files.write(path, content.getBytes(StandardCharsets.UTF_8));
🌐
Java Guides
javaguides.net › 2023 › 09 › java-files-writestring.html
Java Files writeString()
September 28, 2023 - The writeString() method, introduced in Java 11, is a part of the Files class. It's used to write a CharSequence to a file.
🌐
JetBrains
jetbrains.com › help › inspectopedia › ReadWriteStringCanBeUsed.html
'Files.readString()' or 'Files.writeString()' can be used | Inspectopedia Documentation
2 weeks ago - String s = "example"; Files.writeString(Paths.get("out.txt"), s, StandardOpenOption.WRITE); s = Files.readString(Paths.get("in.txt"), StandardCharsets.ISO_8859_1);
🌐
Adam-bien
adam-bien.com › roller › abien › entry › java_7_writing_a_string
Java 7+: Writing A String To File--A One Liner
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class StringToFile { public static void main(String[] args) throws IOException { String msg = "hello"; Files.write(Paths.get("./duke.txt"), msg.getBytes()); } }
🌐
How to do in Java
howtodoinjava.com › home › i/o › java write to file: clean code vs. performance
Java Write to File: Clean Code vs. Performance
October 12, 2023 - Path filePath = Path.of("demo.txt"); String content = "hello world !!"; Files.writeString(filePath, content); Files class is another method write() since Java 7 and it works similarly to writeString().
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-write-to-file
Java Write to File - 4 Ways to Write File in Java | DigitalOcean
August 3, 2022 - FileWriter: FileWriter is the simplest way to write a file in Java. It provides overloaded write method to write int, byte array, and String to the File. You can also write part of the String or byte array using FileWriter.
🌐
Mkyong
mkyong.com › home › java › java – how to save a string to a file
Java - How to save a String to a File - Mkyong.com
May 7, 2019 - package com.mkyong; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; public class StringToFileJava11 { public static void main(String[] args) { String content = "Hello World \r\nJava!\r\n"; String path = "c:\\projects\\app.log"; try { // Java 11 , default StandardCharsets.UTF_8 Files.writeString(Paths.get(path), content); // encoding // Files.writeString(Paths.get(path), content, StandardCharsets.US_ASCII); // extra options // Files.writeString(Paths.get(path), content, // StandardOpenOption.CREATE, StandardOpenOption.APPEND); } catch (IOException e) { e.printStackTrace(); } } }
🌐
Educative
educative.io › answers › what-is-fileswritestring-in-java
What is Files.writeString in Java?
In Java 11, a new method called Files.writeString was introduced to write ... public static Path writeString​(Path path, CharSequence csq, OpenOption... options) throws IOException · public static Path writeString​(Path path, CharSequence csq, Charset cs, OpenOption... options) throws IOException · Path: Path object points to the file in which the string is to be written.
🌐
Jsparrow
jsparrow.github.io › rules › use-files-write-string.html
Use Files.writeString | jSparrow Documentation
Java 11 introduced Files.writeString(Path, CharSequence, Charset, OpenOption...) (opens new window) and Files.writeString(Path, CharSequence, OpenOption...) (opens new window) for writing text into a file by one single invocation and in an efficient non-blocking manner.
🌐
Sentry
sentry.io › sentry answers › java › how to create a file and write to it in java
How to create a file and write to it in Java | Sentry
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class Main { public static void main(String[]args) { String stringToWrite = "\nJava files are easy"; try { BufferedWriter writer = new BufferedWriter(new FileWriter("newfile.txt", true)); writer.write(stringToWrite); writer.close(); } catch (IOException ioe) { System.out.println("Couldn't write to file"); } } } The code above will append a new line saying “Java files are easy” to the newfile.txt each time it is run.
🌐
HappyCoders.eu
happycoders.eu › java › how-to-write-files-quickly-and-easily
How to Write Files Quickly and Easily (Java Files Tutorial)
November 29, 2024 - In Java 7, the utility class java.nio.file.Files was added with the "NIO.2 File API" (NIO stands for New I/O). This class contains methods to write a byte array, a String, or a list of Strings to a file with a single command.