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
🌐
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 first method is equivalent to writeString(path, string, StandardCharsets.UTF_8, options). The second method does the same but only using the specified charset. The options specifies how the file is opened. Java program to write String into a file 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 - Like append the string to the file, overwrite everything in the file with the current string, etc · Return Value: This method does not return any value. Below are two overloaded forms of the writeString() method. public static Path writeString​(Path path, CharSequence csq, OpenOption...
🌐
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
🌐
JetBrains
jetbrains.com › help › inspectopedia › ReadWriteStringCanBeUsed.html
'Files.readString()' or 'Files.writeString()' can be used | Inspectopedia Documentation
String s = "example"; Files.writeString(Paths.get("out.txt"), s, StandardOpenOption.WRITE); s = Files.readString(Paths.get("in.txt"), StandardCharsets.ISO_8859_1);
🌐
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.
🌐
Java Guides
javaguides.net › 2023 › 09 › java-files-writestring.html
Java Files writeString()
September 28, 2023 - import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.io.IOException; public class WriteStringExample { 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); System.out.println("Content written to the file successfully!"); } catch (IOException e) { e.printStackTrace(); } } }
🌐
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.
Find elsewhere
🌐
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 - If the file is recognized then the content type is returned. If the file is not recognized by any of the installed file type detectors then a system-default file type detector is invoked to guess the content type. A given invocation of the Java virtual machine maintains a system-wide list of file type detectors.
🌐
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.jupiter.api.Assertions.assertEquals; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import org.junit.jupiter.api.Test; //... @Test public void writeString() 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); }
🌐
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 }
🌐
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 - It is just as easy to write a string to a file – but only since Java 11: String fileName = ...; String text = ...; Files.writeString(Path.of(fileName), text);Code language: Java (java) Often you don't write a single String into a text file, but several Strings as lines.
🌐
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 ·
🌐
Baeldung
baeldung.com › home › java › java io › java – write to file
Java - Write to File | Baeldung
December 1, 2023 - We’ll make use of BufferedWriter, PrintWriter, FileOutputStream, DataOutputStream, RandomAccessFile, FileChannel, and the Java 7 Files utility class.
🌐
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().
🌐
Marco Behler
marcobehler.com › guides › java-files
How To Work With Files In Java
December 9, 2020 - If you want to write bytes to a file (and in older Java versions < 11 you’d have to use the same API for writing strings), you need to call Files.write. Path anotherUtf8File = Files.createTempFile("some", ".txt"); Files.writeString(anotherUtf8File, "this is my string ää öö üü", StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE); System.out.println("anotherUtf8File = " + anotherUtf8File); Path oneMoreUtf8File = Files.createTempFile("some", ".txt"); Files.write(oneMoreUtf8File, "this is my string ää öö üü".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE); System.out.println("oneMoreUtf8File = " + oneMoreUtf8File); When calling either of the write methods, the file will automatically be created (and truncated if it already exists).
🌐
HowDev
how.dev › answers › what-is-fileswritestring-in-java
What is Files.writeString in Java?
Introduced in Java 11, Files.writeString writes CharSequence to a file, with optional Charset and mode settings.
🌐
Baeldung
baeldung.com › home › java › java io › writing a list of strings into a text file
Writing a List of Strings Into a Text File | Baeldung
April 3, 2025 - In this quick tutorial, we’ll write a List of Strings into a text file in Java in different ways. First, we’ll discuss FileWriter, then BufferedWriter, and finally, Files.writeString.