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

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

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

Copyout.println(text);

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

Copyout.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:

Copytry (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 - Below is the implementation of ... "temp", "gfg.txt"); try { // Write content to file Files.writeString(filePath, "Hello from GFG !!", StandardOpenOption.APPEND); // Verify file content String content = Files.readString...
🌐
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...
🌐
Java Guides
javaguides.net › 2023 › 09 › java-files-writestring.html
Java Files writeString()
September 28, 2023 - import java.nio.file.Files; import ... { public static void main(String[] args) { Path filePath = Paths.get("example.txt"); String content = "Java Files.writeString() Example"; try { Files.writeString(filePath, content, StandardOpenOption.CREATE); ...
🌐
JetBrains
jetbrains.com › help › inspectopedia › ReadWriteStringCanBeUsed.html
'Files.readString()' or 'Files.writeString()' can be used | Inspectopedia Documentation
Example: String s = "example"; Files.write(Paths.get("out.txt"), s.getBytes(StandardCharsets.UTF_8), StandardOpenOption.WRITE); s = new String(Files.readAllBytes(Paths.get("in.txt")), StandardCharsets.ISO_8859_1); String s = "example"; ...
🌐
Jsparrow
jsparrow.github.io › rules › use-files-write-string.html
Use Files.writeString | jSparrow Documentation
try { Files.writeString(Paths.get("/home/test/testpath"), "Hello World!", Charset.defaultCharset()); } catch (IOException ioException) { logError("File could not be written.", ioException); }
🌐
Baeldung
baeldung.com › home › java › java io › java – write to file
Java - Write to File | Baeldung
December 1, 2023 - Looking at the common usage practices, we can see, for example, that PrintWriter is used to write formatted text, FileOutputStream to write binary data, DataOutputStream to write primitive data types, RandomAccessFile to write to a specific position, and FileChannel to write faster in larger files. Some of the APIs of these classes do allow more, but this is a good place to start. This article illustrated the many options of writing data to a file using Java...
🌐
Adam-bien
adam-bien.com › roller › abien › entry › java_11_write_a_string
Java 11: Write a String To File
May 19, 2020 - import static org.junit.jupite... throws IOException { Path fileName = Path.of("celebration.txt"); String content = "duke is 25"; Files.writeString(fileName, content); String actual = Files.readString(fileName); assertEquals(content,actual); }...
Find elsewhere
🌐
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 11, we can use the new API named Files.writeString to write a String or text directly to a file.
🌐
Stack Abuse
stackabuse.com › java-save-write-string-into-a-file
Java: Save/Write String Into a File
August 4, 2020 - Path path = Paths.get("output.txt"); String contents = "Hello"; try { Files.writeString(path, contents, StandardCharsets.UTF_8); } catch (IOException ex) { // Handle exception } There is little room for flexibility here, but it works great if you need to write something down into a file quickly.
🌐
Educative
educative.io › answers › what-is-fileswritestring-in-java
What is Files.writeString in Java?
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. CharSequence: A sequence of characters. All the characters of the CharSequence are written on the file, including line separators.
🌐
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(); } } } c:\\projects\\app.log ·
🌐
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 - Call the Files.writeString() method with an instance, string and characters set. ... // Java Program to Save a String to a File // Using Files.writeString() method // Importing required classes import java.io.*; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Creating an instance of file Path path = Paths.get("C:\\Users\\HP\\Desktop\\gfg.txt"); // Custom string as an input String str = "Geeks
🌐
Ramesh Fadatare
rameshfadatare.com › home › files.readstring() and files.writestring() in java
Files.readString() and Files.writeString() in Java
August 6, 2024 - import java.io.IOException; import ... file Path filePath = Paths.get("example.txt"); // Define the content to write to the file String content = "Hello, Java!\nThis is a sample file."; try { // Write the string to the file Files.writeString(filePath, content, 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 - As the name suggests, writeString() method is used to write the character/text data into files. All characters are written as they are, including the line separators. No extra characters are added. By default, UTF-8 character encoding is used. It throws IOException if an I/O error occurs when writing to or creating the file...
🌐
Marco Behler
marcobehler.com › guides › java-files
How To Work With Files In Java
December 9, 2020 - Path utfFile = Files.createTempFile("some", ".txt"); Files.writeString(utfFile, "this is my string ää öö üü"); // UTF 8 System.out.println("utfFile = " + utfFile); Path iso88591File = Files.createTempFile("some", ".txt"); Files.writeS...
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › nio › file › Files.html
Files (Java SE 11 & JDK 11 )
January 20, 2026 - For example, suppose we want to iterate over the files ending with ".java" in a directory:
🌐
Tutorialspoint
tutorialspoint.com › java › io › filewriter_write_string.htm
Java - FileWriter write(String str, int offset, int len) method
package com.tutorialspoint; import java.io.FileWriter; import java.io.IOException; public class FileWriterDemo { public static void main(String[] args) { try { FileWriter writer = new FileWriter("example.txt"); String data = "Hello Java Programming"; writer.write(data, 6, 4); // Writes "Java" ...
🌐
CodeGym
codegym.cc › java blog › java io & nio › java – write to file
Java – Write to File
December 26, 2024 - The writeString() method is a convenient way to write a String to a file using Java 11 or later. The PrintWriter class automatically flushes the output buffer after each line is written, ensuring that the data is immediately written to the file. This makes it a convenient choice for writing large amounts of text data to a file. Here is an example...