Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What are the advantages of java.nio.file.Files and Path over java.io.File? Give three methods that File lacks.

Files and Path, added in Java 7 as NIO.2, report failures as IOExceptions that say what went wrong, where many java.io.File methods just return false. Path works with any FileSystem, including a ZIP file opened as a file system, and the API supports symbolic links and attributes such as POSIX permissions. Three things File lacks are Files.walk for streaming a directory tree, Files.move with ATOMIC_MOVE, and Files.readAttributes; Files.copy with options is a fourth. We convert between the two with file.toPath() and path.toFile().

Why this question is asked

Nearly every Java codebase still contains java.io.File, because it has been there since Java 1.0 and old examples use it. Yet for new code the Files and Path API is better in ways that matter in production: clear error messages, atomic file replacement, attributes and links. Interviewers ask this question to see whether we know why the newer API exists, not just that it does. In this answer, we will recap the two APIs, compare how they report errors, look at file systems and links, show three operations File cannot do, and see how to mix the two when old code is involved.

1A Quick Recap of File and Path

java.io.File is a class that represents a file name and offers operations on the file it names: exists(), delete(), mkdirs(), renameTo(), listFiles() and so on. It mixes two jobs in one class, describing a path and acting on the file system, and it was designed before Java had a good model for errors in file operations.

Java 7 split those jobs. Path is a value that represents a location, made of name elements, and belongs to a particular FileSystem. Files is a utility class of static methods that act on paths: read, write, copy, move, delete, list, walk and read attributes. We create a path with Path.of("logs", "app.log") (or Paths.get on older code) and resolve and normalise it without touching the disk.

2Errors You Can Act On

The most practical difference is error reporting. Many File methods return a boolean and give no reason when they fail.

Java
File f = new File("/var/data/report.csv");
boolean ok = f.delete();
System.out.println(ok); // false (why? no idea)
Path p = Path.of("/var/data/report.csv");
Files.delete(p);
// java.nio.file.NoSuchFileException: /var/data/report.csv
// or java.nio.file.AccessDeniedException: /var/data/report.csv
// or java.nio.file.DirectoryNotEmptyException: /var/data

Notice that File.delete() returns false for a missing file, a permission problem, a non-empty directory or a file locked by another process, and the caller cannot tell which. Code then either ignores the boolean, which is common and hides bugs, or logs a useless message. Files.delete throws a specific subclass of IOException with the path in the message, so logs say what happened and code can handle each case. The same holds for renameTo against Files.move and mkdirs against Files.createDirectories. When we do want the quiet behaviour, there are explicit methods like Files.deleteIfExists.

3File Systems, Links and Attributes

A Path belongs to a FileSystem, and the default one is the operating system's. But other providers exist. The JDK includes a ZIP file system provider, so we can open a JAR or ZIP file and use the same Files methods to read and write entries inside it, as if it were a directory. Libraries offer in-memory file systems, which make tests fast and isolated, and cloud providers can be plugged in the same way.

The API also understands symbolic links, which File largely ignores. Files.isSymbolicLink, Files.readSymbolicLink and Files.createSymbolicLink work with links directly, and many methods take LinkOption.NOFOLLOW_LINKS to act on the link rather than its target. Attributes are first class too: creation and modification times, size, owner and, on Unix-like systems, POSIX permissions such as rw-r-----, which we can read and set.

Java
try (FileSystem zip = FileSystems.newFileSystem(Path.of("bundle.zip"))) {
Path inside = zip.getPath("/config/app.yml");
System.out.println(Files.readString(inside, StandardCharsets.UTF_8)); // contents of the entry
}
Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rw-r-----");
Files.setPosixFilePermissions(Path.of("secret.key"), perms);

4Three Things File Cannot Do

First, Files.walk(start) returns a lazy Stream<Path> of a whole directory tree, which we can filter and process with the stream API; with File we write our own recursion over listFiles(), which also loads each directory fully into an array. Files.list and Files.newDirectoryStream give a lazy view of a single directory.

Second, Files.move(source, target, StandardCopyOption.ATOMIC_MOVE) renames a file atomically, so other processes see either the old file or the new one, never a half-written one; that is the basis of safe file replacement. File.renameTo has no such guarantee and, again, just returns false on failure. Files.copy accepts options such as REPLACE_EXISTING and COPY_ATTRIBUTES, and can copy from an InputStream to a path.

Third, Files.readAttributes(path, BasicFileAttributes.class) reads size, timestamps and type in one call, which is both faster and consistent, where File needs separate calls for each property.

Java
try (Stream<Path> tree = Files.walk(Path.of("logs"))) {
long big = tree.filter(Files::isRegularFile)
.filter(p -> p.toString().endsWith(".log"))
.count();
System.out.println(big); // 42
}
Files.move(Path.of("state.tmp"), Path.of("state.json"),
StandardCopyOption.ATOMIC_MOVE); // readers see old or new, never half
BasicFileAttributes a = Files.readAttributes(Path.of("state.json"), BasicFileAttributes.class);
System.out.println(a.size() + " " + a.lastModifiedTime()); // 2048 2026-11-04T09:12:31Z

Notice the try-with-resources around Files.walk. Its stream holds open directory handles, so we must close it; the same applies to Files.list and Files.lines.

5Mixing the Two in Existing Code

We do not need to rewrite old code to use the new API. file.toPath() converts a File to a Path, and path.toFile() goes the other way when calling a library that still wants a File; toFile() only works for paths on the default file system. A good habit is to accept Path in new methods and convert at the edges where old APIs require File.

6Follow-up questions

Is Paths.get deprecated?

Not formally deprecated, but since Java 11 Path.of is the recommended way, and the Paths class documentation says it may be deprecated in a future release. Both return the same kind of Path from the default file system.

Does Files.exists followed by an operation avoid errors?

No. Between the check and the operation another process can create or delete the file, which is a race known as time-of-check to time-of-use. It is better to just perform the operation and handle the specific exception, or to use options such as CREATE_NEW, which fail atomically if the file already exists.

7Conclusion

In this answer, we saw that Files and Path improve on java.io.File by throwing specific IOExceptions instead of returning false, by working with any FileSystem including ZIP files, and by supporting symbolic links and attributes such as POSIX permissions. Files.walk, Files.move with ATOMIC_MOVE, Files.copy with options and Files.readAttributes have no File equivalent, and toPath() and toFile() let us use both while migrating.

What to say in the interviewAbout 80 seconds aloud

Open with

The biggest practical difference is error handling. Many File methods, like delete, renameTo and mkdirs, return false on failure and never say why, so I cannot tell a missing file from a permission problem or a non-empty directory.

Then, if they want more

Files methods throw specific exceptions like NoSuchFileException or AccessDeniedException with the path in the message, so logs are useful and code can react properly. Second, Path belongs to a FileSystem, so the same API works on a ZIP file opened with FileSystems.newFileSystem, or on an in-memory file system in tests.

Third, it understands symbolic links and file attributes, including POSIX permissions, which File basically ignores.

Three methods File does not have: Files.walk, which gives a lazy stream over a whole directory tree, which I close with try-with-resources; Files.move with ATOMIC_MOVE, which replaces a file so readers see either the old or the new version, never a half-written one, and Files.copy with options like REPLACE_EXISTING; and Files.readAttributes, which reads size, times and type in one call.

File and Path are easy to mix: file.toPath and path.toFile convert between them, so in old code I accept Path in new methods and convert only at the edges.

About this answer

Last updated
12 November 2026
Versions it names
Java 7, Java 11
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What are the advantages of java.nio.file.Files and Path over java.io.File? Give three...": https://scrivio-interview-library.netlify.app/java/io-and-nio/91f1ebdff1/