Java Reference ยท I/O and Files
Files.writeString() and write()
Reference for writing files with java.nio.file.Files: writeString, write for bytes and lines, and the OpenOption flags CREATE, APPEND and TRUNCATE_EXISTING.
Files.writeString(path, text) (Java 11+) writes a string to a file, and Files.write(path, bytes) or Files.write(path, lines) writes a byte array or a list of lines. Each call opens the file, writes, and closes it again.
By default the file is created if it does not exist and truncated (emptied first) if it does. You change this by passing StandardOpenOption values: APPEND to add to the end, CREATE_NEW to fail if the file already exists, and so on.
Files.writeString(Path.of("out.txt"), "hello\n"); Files.writeString(Path.of("out.txt"), "more\n", StandardOpenOption.APPEND);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
static Path writeString(Path path, CharSequence csq, OpenOption... options) throws IOException static Path writeString(Path path, CharSequence csq, Charset cs, OpenOption... options) throws IOException static Path write(Path path, byte[] bytes, OpenOption... options) throws IOException static Path write(Path path, Iterable<? extends CharSequence> lines, OpenOption... options) throws IOException
| Name | Description |
|---|---|
path |
The target file. Its parent directory must already exist. |
csq / bytes / lines |
The content. For lines, each element is followed by the platform line separator. |
cs |
Charset for encoding (UTF-8 by default). |
options |
StandardOpenOption flags. With none, the behavior is CREATE, TRUNCATE_EXISTING, WRITE. |
Returns: the same Path that was passed in.
Throws: IOException and subclasses such as NoSuchFileException (missing parent), FileAlreadyExistsException (CREATE_NEW), AccessDeniedException. UnmappableCharacterException if the text cannot be encoded in the charset.
Usage notes
- Truncation is silent. Forgetting
APPENDis the classic way to lose a file's contents. - If you pass any options, you replace the defaults.
writeString(p, s, APPEND)appends, but it will not create a missing file: a loneAPPENDorWRITEthrowsNoSuchFileException. PassCREATE, APPENDto append to a file that may not exist yet. - Line separator.
write(path, lines)uses the platform separator (\non Linux,\r\non Windows);writeStringwrites exactly the text you give it. - Not atomic. A crash in the middle can leave a half-written file. For safe replacement, write to a temporary file and then move it with
ATOMIC_MOVE(Files.move). - Many small writes are slow because each call reopens the file. For a loop, use a
BufferedWriter. - Directories: create parents first with
Files.createDirectories. StandardOpenOption.SYNCandDSYNCask the OS to flush to disk; they are slow and rarely needed.