Java Files Methods

About This Index
This page is the Java Files Methods index: every guide for the static methods of java.nio.file.Files, each with syntax, parameters, return value, exceptions, operating-system notes, three runnable examples, and FAQs.
Learn what the Files class is, how Path fits in, and which exceptions to expect on the Files hub. Use this page when you need a specific method such as readAllLines(), write(), or walk(). All examples compile on Java 8 and later, except the readString() and writeString() pages, which need Java 11.
The examples work inside a fresh folder from Files.createTempDirectory("demo"), print only file names and relative paths, and delete everything they created before they finish.
How to Call a Files Method
Every method is static: build a Path, then pass it to Files. Most methods throw IOException:
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Path file = Paths.get("notes.txt"); // Path.of("notes.txt") on Java 11+
Files.write(file, "Hello".getBytes("UTF-8")); // create or overwrite
boolean there = Files.exists(file); // true
long bytes = Files.size(file); // 5
Files.delete(file); // remove it again Java Files Methods Table
All method guides in this Java tutorial series (50 tutorials), grouped by purpose.
Create files, folders, and links
7 methodsMake new empty files and directories, temporary files and folders, hard links, and symbolic links.
| Method | Description | Tutorial |
|---|---|---|
createFile() | Create a new, empty file, failing if anything with that name already exists. | Try it |
createDirectory() | Create one new folder whose parent already exists; fails if the name is taken. | Try it |
createDirectories() | Create a folder and every missing parent; no error if the folder already exists. | Try it |
createTempFile() | Create a new empty file with a unique random name in the temp folder or a folder you choose. | Try it |
createTempDirectory() | Create a new empty folder with a unique random name: a safe scratch space for any task. | Try it |
createLink() | Create a hard link: a second name for the same file data on the same drive. | Try it |
createSymbolicLink() | Create a symbolic link that points to another path (needs extra rights on Windows). | Try it |
Copy, move, and delete
4 methodsCopy or rename files, move them between folders, and remove files or empty directories.
| Method | Description | Tutorial |
|---|---|---|
copy() | Copy a file to a new path, or copy between a file and an InputStream or OutputStream. | Try it |
move() | Rename a file or folder, or move it to another directory, with optional replace and atomic modes. | Try it |
delete() | Delete a file or an empty folder; throws if it is missing or the folder is not empty. | Try it |
deleteIfExists() | Delete a file or empty folder if it exists; returns true if something was deleted. | Try it |
Check files and paths
11 methodsAsk whether a path exists, what kind of entry it is, what you may do with it, and how big it is.
| Method | Description | Tutorial |
|---|---|---|
exists() | Check whether a file or folder exists (following symbolic links by default). | Try it |
notExists() | Check that a path definitely does not exist; false when unsure. | Try it |
isDirectory() | Check whether a path exists and is a folder. | Try it |
isRegularFile() | Check whether a path exists and is an ordinary file (not a folder or device). | Try it |
isReadable() | Check whether a file exists and this program is allowed to read it. | Try it |
isWritable() | Check whether a file exists and this program may change it. | Try it |
isExecutable() | Check whether a file may be run as a program, or a folder may be entered. | Try it |
isHidden() | Check whether a file counts as hidden: a dot name on Linux/macOS, the hidden attribute on Windows. | Try it |
isSameFile() | Check whether two paths lead to the same file, even when they are written differently. | Try it |
isSymbolicLink() | Check whether a path itself is a symbolic link, whether or not its target exists. | Try it |
size() | Get the size of a file in bytes as a long. | Try it |
Read files
7 methodsLoad a whole file as text, lines, or bytes, or open a reader, stream, or channel to read it step by step.
| Method | Description | Tutorial |
|---|---|---|
readString() | Read a whole text file into one String, UTF-8 by default (Java 11+). | Try it |
readAllLines() | Read a whole text file as a List of lines, without the line breaks. | Try it |
readAllBytes() | Read a whole file into a byte array. | Try it |
lines() | Read a text file lazily as a Stream of lines; close it with try-with-resources (Java 8+). | Try it |
newBufferedReader() | Open a text file as a BufferedReader for line-by-line reading. | Try it |
newInputStream() | Open a file as an InputStream for reading raw bytes. | Try it |
newByteChannel() | Open a file as a SeekableByteChannel to read and write at any position. | Try it |
Write files
4 methodsSave bytes, lines, or a String in one call, or open a writer or output stream for larger output.
| Method | Description | Tutorial |
|---|---|---|
write() | Write a byte array or a list of lines to a file in one call. | Try it |
writeString() | Write a String or other CharSequence to a file, UTF-8 by default (Java 11+). | Try it |
newBufferedWriter() | Open a file as a BufferedWriter for writing text efficiently. | Try it |
newOutputStream() | Open a file as an OutputStream for writing raw bytes. | Try it |
List and walk directories
5 methodsList one folder, walk a whole tree, search it with a filter, or visit every entry with a FileVisitor.
| Method | Description | Tutorial |
|---|---|---|
list() | Get a lazy Stream of the entries directly inside a folder (Java 8+). | Try it |
walk() | Stream every file and folder in a directory tree, depth-first (Java 8+). | Try it |
find() | Search a directory tree for paths matching a condition on name and attributes (Java 8+). | Try it |
walkFileTree() | Visit every file and folder in a tree with a FileVisitor that can skip, stop, or handle errors. | Try it |
newDirectoryStream() | Iterate the entries of one folder with a for-each loop, optionally filtered by glob or Filter. | Try it |
Attributes, owners, and permissions
12 methodsRead and change timestamps, sizes, owners, and POSIX permissions, and inspect stores, content types, and links.
| Method | Description | Tutorial |
|---|---|---|
getAttribute() | Read one file attribute by name, such as "basic:size" or "dos:hidden". | Try it |
setAttribute() | Change one file attribute by name, such as a time or "dos:readonly". | Try it |
readAttributes() | Read many file attributes in one call, as a typed object or a Map. | Try it |
getLastModifiedTime() | Get a file's last-modified time as a FileTime. | Try it |
setLastModifiedTime() | Change a file's last-modified time to any FileTime. | Try it |
getOwner() | Get the user (or group) that owns a file, as a UserPrincipal. | Try it |
setOwner() | Change the owner of a file to another UserPrincipal (usually needs admin or root). | Try it |
getPosixFilePermissions() | Read a file's Unix permission bits as a Set of PosixFilePermission (Linux, macOS). | Try it |
setPosixFilePermissions() | Set a file's Unix permission bits, like chmod (Linux, macOS). | Try it |
getFileStore() | Get the FileStore (drive, partition, or volume) that holds a file. | Try it |
probeContentType() | Guess a file's MIME type, such as text/plain or image/png; may return null. | Try it |
readSymbolicLink() | Read the target stored in a symbolic link, without following it. | Try it |
Frequently Asked Questions
Start with createTempDirectory()
Make a safe scratch folder for experiments, then delete it when you are done.
11 people found this page helpful
