Home Java Files Methods writeString() Java Files writeString() Method Definition In Java, Files.writeString(path, text) writes a CharSequence (a String, StringBuilder, and so on) to a file, encoded as UTF-8 . It writes exactly the characters you give it; no line separator is added.
Like write, it creates the file or replaces its content unless you pass options such as APPEND. It is the writing half of readString.
A static method of the final utility class java.nio.file.Files, available since Java 11. Call it on the class name, as in Files.writeString(...), after import java.nio.file.Files;. You never create a Files object.
Requires Java 11 or later. On Java 8, encode the text yourself: Files.write(path, text.getBytes(StandardCharsets.UTF_8)). Example 3 shows that both produce the same bytes.
Syntax Path, text, and optionally a charset and options:
Files.writeString(path, "Hello"); // UTF-8, replace
Files.writeString(path, "more\n", StandardOpenOption.APPEND);
Files.writeString(path, text, StandardCharsets.ISO_8859_1);
Files.write(path, "Hello".getBytes(StandardCharsets.UTF_8)); // Java 8 Call What it does Files.writeString(p, s)Replace the file with s in UTF-8. Files.writeString(p, s, APPEND)Add s to the end. Files.writeString(p, s, cs)Encode with charset cs.
Since Java 11. Characters that cannot be encoded cause an exception instead of being replaced with ?.
Library declarations public static Path writeString(Path path, CharSequence csq, OpenOption... options) throws IOException
public static Path writeString(Path path, CharSequence csq, Charset cs, OpenOption... options) throws IOException Parameters Parameter Description pathThe file to write. csqThe text to write. cs(Second overload) Charset for encoding. optionsOpen options; default CREATE, TRUNCATE_EXISTING, WRITE.
Return Value Return Value Description PathThe path you passed in.
Important
Throws UnmappableCharacterException (a CharacterCodingException, which is an IOException) when a character does not exist in the charset, NoSuchFileException if the parent folder is missing, and FileAlreadyExistsException with CREATE_NEW.
Compare with Similar Methods Text writing:
Java Examples 1. Write and read back 2. Append entries 3. Java 8 alternative and errors Example 1 — Write One String to a File Write a greeting, check the size, and read it back.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class WriteStringBasic {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path hello = dir.resolve("hello.txt");
Path written = Files.writeString(hello, "Hello, Java 11!");
System.out.println("Wrote: " + written.getFileName());
System.out.println("Size: " + Files.size(hello));
System.out.println("Content: " + Files.readString(hello));
Files.delete(hello);
Files.delete(dir);
}
} Run with java WriteStringBasic. Requires Java 11 or later; the output is the same on Windows, macOS, and Linux.
Wrote: hello.txt
Size: 15
Content: Hello, Java 11! How It Works Returns the path. writeString hands back the same Path, so the program prints its file name.Nothing added. The 15 ASCII characters become 15 bytes; no line break is appended, so the size is identical on every system.Round trip. readString decodes the bytes again and returns the original text.Example 2 — Append Two Log Entries Write the first entry, then append a second one with StandardOpenOption.APPEND.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
public class WriteStringAppend {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path log = dir.resolve("events.log");
Files.writeString(log, "start\n");
Files.writeString(log, "stop\n", StandardOpenOption.APPEND);
System.out.println("Lines: " + Files.readAllLines(log));
System.out.println("Size: " + Files.size(log));
Files.delete(log);
Files.delete(dir);
}
} Run with java WriteStringAppend. Requires Java 11 or later; the output is the same on Windows, macOS, and Linux.
Lines: [start, stop]
Size: 11 How It Works Your own line breaks. Each string ends with \n, written as given, so the size does not depend on the operating system.APPEND keeps the first entry. Without it, the second call would truncate the file and leave only "stop".Count the bytes. "start\n" is 6 bytes and "stop\n" is 5, giving 11.Example 3 — Java 8 Equivalent and an Unmappable Character Compare the bytes from writeString with the Java 8 Files.write form, then try to save e-acute as US-ASCII.
import java.io.IOException;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
public class WriteStringJava8 {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path modern = dir.resolve("modern.txt");
Path classic = dir.resolve("classic.txt");
String text = "Price: 5 EUR";
Files.writeString(modern, text);
Files.write(classic, text.getBytes(StandardCharsets.UTF_8));
System.out.println("Same bytes: " + Arrays.equals(Files.readAllBytes(modern), Files.readAllBytes(classic)));
try {
Files.writeString(modern, "caf\u00e9", StandardCharsets.US_ASCII);
} catch (CharacterCodingException e) {
System.out.println("US-ASCII: " + e.getClass().getSimpleName());
}
Files.delete(modern);
Files.delete(classic);
Files.delete(dir);
}
} Run with java WriteStringJava8. Requires Java 11 or later; the output is the same on Windows, macOS, and Linux.
Same bytes: true
US-ASCII: UnmappableCharacterException How It Works Two routes, one result. writeString and Files.write(path, text.getBytes(UTF_8)) store identical bytes, so the Java 8 form is a safe drop-in.No silent question marks. US-ASCII has no e-acute. String.getBytes would quietly write ?, but writeString throws UnmappableCharacterException.Catch the parent type. Catching CharacterCodingException also covers malformed input, and printing the simple name shows which one occurred.Frequently Asked Questions Which Java version added Files.writeString? Java 11, together with Files.readString.
How do I write a String to a file in Java 8? Files.write(path, text.getBytes(StandardCharsets.UTF_8)).
Does Files.writeString add a newline at the end? No. It writes exactly the characters you pass; add \n or System.lineSeparator() yourself.
How do I append with Files.writeString? Pass StandardOpenOption.APPEND, and CREATE as well if the file may not exist yet.
What happens if a character cannot be encoded? writeString throws UnmappableCharacterException instead of writing a replacement character.
Back to the method index See all 50 Files method guides grouped by what they do.
Method index → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion