Java Files writeString() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 11+

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:

Java
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
CallWhat 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
Java
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

ParameterDescription
pathThe file to write.
csqThe text to write.
cs(Second overload) Charset for encoding.
optionsOpen options; default CREATE, TRUNCATE_EXISTING, WRITE.

Return Value

Return ValueDescription
PathThe path you passed in.

Compare with Similar Methods

Text writing:

MethodHow it differs
write()Java 7; bytes or lines with separators.
newBufferedWriter()Many small writes.
readString()Read the text back (Java 11).

Java Examples

Example 1 — Write One String to a File

Write a greeting, check the size, and read it back.

Java
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);
    }
}

How It Works

  1. Returns the path. writeString hands back the same Path, so the program prints its file name.
  2. Nothing added. The 15 ASCII characters become 15 bytes; no line break is appended, so the size is identical on every system.
  3. 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.

Java
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);
    }
}

How It Works

  1. Your own line breaks. Each string ends with \n, written as given, so the size does not depend on the operating system.
  2. APPEND keeps the first entry. Without it, the second call would truncate the file and leave only "stop".
  3. 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.

Java
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);
    }
}

How It Works

  1. 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.
  2. No silent question marks. US-ASCII has no e-acute. String.getBytes would quietly write ?, but writeString throws UnmappableCharacterException.
  3. Catch the parent type. Catching CharacterCodingException also covers malformed input, and printing the simple name shows which one occurred.

Frequently Asked Questions

Java 11, together with Files.readString.
Files.write(path, text.getBytes(StandardCharsets.UTF_8)).
No. It writes exactly the characters you pass; add \n or System.lineSeparator() yourself.
Pass StandardOpenOption.APPEND, and CREATE as well if the file may not exist yet.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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