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
🌐
Mkyong
mkyong.com › home › java › java create and write to a file
Java create and write to a file - Mkyong.com
October 2, 2020 - String content = "..."; Files.write(path, content.getBytes(StandardCharsets.UTF_8)); In Java 11, we can use the new API named Files.writeString to write a String or text directly to a file.
🌐
GeeksforGeeks
geeksforgeeks.org › java › files-class-writestring-method-in-java-with-examples
Files Class writeString() Method in Java with Examples - GeeksforGeeks
July 23, 2025 - UTF-8 charset is used to write the content to file in the first method. The second method does the same using the specified charset. How the file is opened is specified in Options.
🌐
Educative
educative.io › answers › what-is-fileswritestring-in-java
What is Files.writeString in Java?
import java.nio.charset.StandardCharsets; public class Main { public static void main(String[] args) { Path filePath = Paths.get("./test.txt"); try { // write content to the files · Files.writeString(filePath, "\nEducative.io", StandardCharsets.UTF_8, StandardOpenOption.APPEND); System.out.println(Files.readString(filePath)); } catch (IOException e) { e.printStackTrace(); } } } In the code above, we have: Created a Path object to point the file to be written (test.txt).
🌐
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 - Files.writeString(path, string, StandardCharsets.UTF_8) ... This article has shown different methods for writing byte arrays and Strings in binary and text files in Java.
🌐
Ramesh Fadatare
rameshfadatare.com › home › files.readstring() and files.writestring() in java
Files.readString() and Files.writeString() in Java
August 6, 2024 - writeString(): The Files.writeString() method writes the specified content to the file using UTF-8 encoding. The StandardOpenOption.CREATE option is used to create the file if it doesn’t exist. Exception Handling: An IOException is caught and handled if any I/O error occurs.
🌐
JetBrains
jetbrains.com › help › inspectopedia › ReadWriteStringCanBeUsed.html
'Files.readString()' or 'Files.writeString()' can be used | Inspectopedia Documentation
Such calls can be replaced with a call to a Files.readString() or Files.writeString() method introduced in Java 11. ... 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);
🌐
HowToDoInJava
howtodoinjava.com › home › i/o › java write to file: clean code vs. performance
Java Write to File: Clean Code vs. Performance
October 12, 2023 - Starting with Java 11, the Files.writeString() method offers a convenient way to write text to a file. It accepts a Path parameter and a string, simplifying the process of writing text content in a single-line statement.
Find elsewhere
🌐
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.
🌐
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.
🌐
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 – 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 ... 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, ...
🌐
Marco Behler
marcobehler.com › guides › java-files
How To Work With Files In Java
December 9, 2020 - Path utfFile = Files.createTem... bug in previous versions), you should be using the Files.writeString method to write string content to a file....
🌐
sqlpey
sqlpey.com › java › java-string-to-file-methods
Java Methods for Writing Strings to Files: A Detailed Exploration - sqlpey
November 4, 2025 - import java.nio.file.*; import ...tion.CREATE, StandardOpenOption.APPEND); This combination is useful for buffered writing of characters, which can improve performance over writing individual characters in a loop....
🌐
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.
🌐
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 - The writeString() method of File Class in Java is used to write contents to the specified file. 'java.nio.file.Files' class is having a predefined writeString() method which is used to write all content to a file, using the UTF-8 charset.
🌐
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); } Pre · try (BufferedWriter bufferedWriter = new BufferedWriter( new FileWriter("/home/test/testpath", StandardCharsets.UTF_8))) { bufferedWriter.write("Hello World!"); } catch (IOException ioException) { logError("File could not be written.", ioException); } Post ·
Top answer
1 of 16
1877

Note that each of the code samples below may throw IOException. Try/catch/finally blocks have been omitted for brevity. See this tutorial for information about exception handling.

Note that each of the code samples below will overwrite the file if it already exists

Creating a text file:

PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
writer.println("The first line");
writer.println("The second line");
writer.close();

Creating a binary file:

byte data[] = ...
FileOutputStream out = new FileOutputStream("the-file-name");
out.write(data);
out.close();

Java 7+ users can use the Files class to write to files:

Creating a text file:

List<String> lines = Arrays.asList("The first line", "The second line");
Path file = Paths.get("the-file-name.txt");
Files.write(file, lines, StandardCharsets.UTF_8);
//Files.write(file, lines, StandardCharsets.UTF_8, StandardOpenOption.APPEND);

Creating a binary file:

byte data[] = ...
Path file = Paths.get("the-file-name");
Files.write(file, data);
//Files.write(file, data, StandardOpenOption.APPEND);
2 of 16
452

In Java 7 and up:

try (Writer writer = new BufferedWriter(new OutputStreamWriter(
              new FileOutputStream("filename.txt"), "utf-8"))) {
   writer.write("something");
}

There are useful utilities for that though:

  • FileUtils.writeStringtoFile(..) from commons-io
  • Files.write(..) from guava

Note also that you can use a FileWriter, but it uses the default encoding, which is often a bad idea - it's best to specify the encoding explicitly.

Below is the original, prior-to-Java 7 answer


Writer writer = null;

try {
    writer = new BufferedWriter(new OutputStreamWriter(
          new FileOutputStream("filename.txt"), "utf-8"));
    writer.write("Something");
} catch (IOException ex) {
    // Report
} finally {
   try {writer.close();} catch (Exception ex) {/*ignore*/}
}

See also: Reading, Writing, and Creating Files (includes NIO2).