File Handling in Java
File handling in Java means working with files and folders on disk: creating them, writing data to them, reading data from them, and copying, moving, renaming and deleting them. Java has two APIs for this. The older java.io.File class has existed since Java 1.0. The modern NIO.2 API, added in Java 7 in the java.nio.file package, uses the Path interface and the Files class and is the recommended choice today. In this lesson, you will get an overview of both and a first complete example.
The Two APIs
java.io.File (legacy) | NIO.2: Path and Files (modern) | |
|---|---|---|
| Added in | Java 1.0 | Java 7 |
| Represents a location with | File | Path |
| Operations live in | Methods of File | Static methods of Files |
| Errors | Many methods just return false | Methods throw descriptive exceptions |
| Reading and writing text | Needs streams | Files.readString, Files.writeString in one line |
| Walking folder trees | Write your own recursion | Files.walk, Files.find |
| Symbolic links, permissions, attributes | Limited | Full support |
Common File Operations at a Glance
| Task | NIO.2 method |
|---|---|
| Point to a file | Path.of("data", "marks.txt") |
| Create a file | Files.createFile(path) |
| Write text | Files.writeString(path, text) |
| Read text | Files.readString(path) |
| Read lines | Files.readAllLines(path), Files.lines(path) |
| Check existence | Files.exists(path) |
| Copy, move, delete | Files.copy, Files.move, Files.delete |
| Create folders | Files.createDirectories(path) |
| List or walk a folder | Files.list(dir), Files.walk(dir) |
Example: A Complete Round Trip
Let's see an example that creates a folder, writes a file, reads it, copies it and cleans up.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
public class RoundTrip {
public static void main(String[] args) throws IOException {
Path dir = Files.createDirectories(Path.of("school"));
Path file = dir.resolve("students.txt");
Files.write(file, List.of("Navika", "Srihitha", "Advitha"));
System.out.println("Exists: " + Files.exists(file)
+ ", size: " + Files.size(file) + " bytes");
List<String> names = Files.readAllLines(file);
System.out.println("Read: " + names);
Path backup = Files.copy(file, dir.resolve("students.bak"));
System.out.println("Backup: " + backup);
Files.delete(backup);
Files.delete(file);
Files.delete(dir);
System.out.println("Cleaned up: " + Files.notExists(dir));
}
}Exists: true, size: 24 bytes
Read: [Navika, Srihitha, Advitha]
Backup: school/students.bak
Cleaned up: trueExplanation: Path.of("school") describes a location; Files.createDirectories creates the folder. resolve builds a path inside it. Files.write writes each name on its own line (6 + 8 + 7 letters plus 3 line breaks = 24 bytes), and readAllLines reads them back. Every operation is a single static call on Files.
Common Mistakes
Mixing up a path and a file. A Path is just a name; the file may or may not exist.
Ignoring IOException. File operations can fail for many reasons: missing files, permissions, full disks.
Hard-coding \ or / in paths. Use Path.of("a", "b") or resolve, which use the right separator.
Interview Corner
What is the difference between java.io.File and java.nio.file.Path?
java.io.File is the original Java 1.0 class representing a file path together with operations on it; many of its methods return boolean instead of throwing exceptions, and it has limited support for links, permissions and attributes. java.nio.file.Path, added in Java 7 as part of NIO.2, represents only the path, while the Files utility class provides operations that throw descriptive exceptions, support symbolic links, attributes, permissions, directory walking and simple one-line reading and writing. New code should use Path and Files; file.toPath() and path.toFile() convert between them.
Practice
- Easy: Create a folder
notes, write a file with three lines into it, and print the number of lines. - Harder: Write a program that creates
backup/2026-11-12/(using today's date) and copies a file into it.
Key Takeaways
Was this tutorial helpful?