With Java 7 and up, a one liner using Files:

String text = "Text to save to file";
Files.write(Paths.get("./fileName.txt"), text.getBytes());
Answer from dazito 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:
🌐
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.
🌐
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 - Learn to write text and binary data into files using Java Writer, FileChannel, ByteBuffer, Files.write() and writeString() methods in Java 8 and Java 11. ... When working on an enterprise application, sometimes it is necessary to write the text ...
🌐
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.
🌐
CodeGym
codegym.cc › java blog › java io & nio › java – write to file
Java – Write to File
December 26, 2024 - We then create a FileChannel object named channel, which is initialized using randomAccessFile object. We then create a ByteBuffer object named buffer with a capacity of 1024 bytes and put the textToWrite string as the parameter. We then flip the buffer object to prepare it for writing and write it to the channel object using the write() method...
🌐
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 org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; public class CommonsIOExample { public static void main(String[] args) { String content = "Hello World \r\nJava!\r\n"; String path = "c:\\projects\\app2.log"; try { FileUtils.writeStringToFile(new File(path), content, StandardCharsets.UTF_8); // append // FileUtils.writeStringToFile(new File(path), content, StandardCharsets.UTF_8, true); } catch (IOException e) { e.printStackTrace(); } } }
🌐
iO Flood
ioflood.com › blog › java-write-to-file
How to Write to Files in Java: A Step-by-Step Guide
February 20, 2024 - Here’s how you can write to a file using UTF-8 encoding: import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.IOException; import java.nio.charset.StandardCharsets; public class Main { public static void main(String[] args) { try { FileOutputStream fileOut = new FileOutputStream("output.txt"); OutputStreamWriter writer = new OutputStreamWriter(fileOut, StandardCharsets.UTF_8); writer.write("Hello, world!"); writer.close(); } catch (IOException e) { e.printStackTrace(); } } } # Output: # 'Hello, world!' is written to 'output.txt' in UTF-8 encoding
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"); 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.
🌐
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 - What are we doing here? The Files.write() method used here is the same as in the previous example, that is, the one that accepts an Iterable<String>. A Java 8 Stream itself is not an Iterable since you cannot iterate over it multiple times, but only once.
🌐
TutorialKart
tutorialkart.com › java › java-write-string-to-file
Write String to File in Java
January 10, 2023 - Use BufferedOutputStream.write() to write the byte array to file. Close BufferedOutputStream and FileOutputStream to release any system resources associated with the streams. We have the file contents read to string.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-write-into-a-file
Java Program to Write into a File - GeeksforGeeks
July 23, 2025 - // Write File using // writeString Method import java.io.*; import java.nio.file.Files; import java.nio.file.Path; public class WriteString { public static void main(String[] args) throws IOException { // Data to be written in file String text ...
🌐
Octoperf
octoperf.com › blog › 2018 › 03 › 21 › java-write-to-file
Java - 10+ Amazing Ways to Write to File - OctoPerf
In this example, the Hello World! string is written using UTF-8 charset. Then we check the file contains the expected string. We have to use the bridge File.toPath to convert the legacy File created by the JUnit TemporaryFolder into a Path. To write multiple lines into a single file, you can ...
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Java Write String to a File - Java Code Geeks
April 5, 2021 - The BufferedWriter class can be used to write a String to a file more efficiently. The BufferedWriter maintains an internal buffer of 8192 characters. The characters are written to an internal buffer and once the buffer is filled or the writer ...
🌐
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 ...
🌐
Rip Tutorial
riptutorial.com › writing text to a file in utf-8
Java Language Tutorial => Writing text to a file in UTF-8
import java.io.BufferedWriter; ... main(String[] args) throws IOException { //StandardCharsets is available since Java 1.7 //for ealier version use Charset.forName("UTF-8"); try (BufferedWriter wr = Files.newBufferedWriter(Paths.get("test2.txt"), ...
🌐
W3Docs
w3docs.com › java
How do I save a String to a text file using Java? | W3Docs
import java.io.FileWriter; import java.io.IOException; public class Main { public static void main(String[] args) { // Set the file path String filePath = "C:\\folder\\file.txt"; // Set the content to be saved String content = "This is the content to be saved to the file"; try { // Create a FileWriter object FileWriter writer = new FileWriter(filePath); // Write the content to the file writer.write(content); // Close the writer writer.close(); } catch (IOException e) { e.printStackTrace(); } } }
🌐
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 are two overloaded forms of the writeString() method. public static Path writeString​(Path path, CharSequence csq, OpenOption... options) throws IOException · public static Path writeString​(Path path, CharSequence csq, Charset cs, ...