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
🌐
Baeldung
baeldung.com › home › java › java io › java – write to file
Java - Write to File | Baeldung
December 1, 2023 - Let’s now see how we can use FileOutputStream to write binary data to a file. The following code converts a String into bytes and writes the bytes to a file using FileOutputStream:
🌐
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.
🌐
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.
🌐
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 - Learn to write a string into file ... · Java 11 · Files IO · Learn to write a string into a file in Java using Files.writeString() method....
🌐
GeeksforGeeks
geeksforgeeks.org › java › files-class-writestring-method-in-java-with-examples
Files Class writeString() Method in Java with Examples - GeeksforGeeks
July 23, 2025 - Below is the implementation of the problem statement: ... // 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....
🌐
TutorialKart
tutorialkart.com › java › java-write-string-to-file
Write String to File in Java
January 10, 2023 - FileOutputStream and BufferedOutputStream could throw IOException. Hence we used Java Try with Resource. Run the program from command prompt or in your favorite IDE like Eclipse. ... Data written to file successfully. In this example, we shall use Apache’s commons.io package to write string to file.
🌐
W3Schools
w3schools.com › java › java_files_write.asp
Java Write To Files
Tip: To see what's inside the file, go to the Java Read Files chapter. There are several classes you can use to write files in Java:
Find elsewhere
🌐
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.
🌐
Stack Abuse
stackabuse.com › java-save-write-string-into-a-file
Java: Save/Write String Into a File
August 4, 2020 - In this article, we'll show some common methods for writing a String into a file. Here's a list of all the classes and methods we'll go over: ... 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.
🌐
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(); } } }
🌐
TraceDynamics
tracedynamics.com › write-string-to-file-java
Write String to a File Java: Discover Multiple Techniques
December 8, 2023 - There are several ways to write a string to a text file in Java, such as using the BufferedWriter, FileWriter, or Files classes
🌐
iO Flood
ioflood.com › blog › java-write-to-file
How to Write to Files in Java: A Step-by-Step Guide
February 20, 2024 - In this example, we create a PrintWriter object and pass the name of the file we want to write to (‘output.txt’) to the constructor. We then use the println method to write a string to the file, and finally close the writer using the close ...
🌐
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));
🌐
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 - When working on an enterprise application, sometimes it is necessary to write the text or binary data into files in Java e.g. writing user-generated reports into the filesystem. Though there are multiple ways of writing the files in Java, let’s quickly go through a few of them for quick reference when it is needed. String content = "some text"; // Java 11 Files.writeString(Path.of("demo.txt"), content); // Using Files class Files.write(Path.of("demo.txt"), content.getBytes()); // Using BufferedWriter try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath.toFile()))) { writer
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-write-into-a-file
Java Program to Write into a File - GeeksforGeeks
July 23, 2025 - It is useful for writing characters, strings, and arrays. It is better to wrap this class with any writer class for writing data to a file if no prompt output is required. Example: The below example illustrates the use of the BufferedWriter class to write content into a file. ... // Write into a File // Using BufferedWriter Class // Importing java input output libraries import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; public class BufferedWriterClass { public static void main(String[] args) {
🌐
TutorialsPoint
tutorialspoint.com › java-program-to-save-a-string-to-a-file
Java Program to Save a String to a File
July 24, 2024 - The File class includes a utility technique to save a string to a file using Java. It can be implemented by FileswriteString(path, string, options). This method comes with two different process.
🌐
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
There are many different ways to write data to a file in Java, but the easiest way is to use a BufferedWriter along with a FileWriter, as follows. import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class ...
🌐
Sentry
sentry.io › sentry answers › java › how to write to a file in java
How to Write to a File in Java | Sentry
However, the FileOutputStream class ... as text. Of course, you can write textual data to file using a FileOutputStream by simply encoding the String text to bytes....
🌐
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()); } }