Java > Java Input/Output (I/O) > File Handling > Path and Files API
Reading and Writing Text Files Using Path and Files API
This example demonstrates how to read from and write to text files using the modern `Path` and `Files` API in Java. This API offers a more flexible and secure way to handle file operations compared to older approaches.
Code Snippet: Reading and Writing a File
This code snippet first defines the file path using `Paths.get()`. It then demonstrates writing a list of strings to the file using `Files.write()`. Crucially, it specifies the character encoding (UTF-8) to ensure proper handling of characters. Finally, it reads all lines from the file using `Files.readAllLines()`, again specifying the character encoding, and prints them to the console. Error handling is included using try-catch blocks to gracefully manage potential `IOExceptions`.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
import java.util.List;
public class FileReadWriteExample {
public static void main(String[] args) {
// Define file path
Path filePath = Paths.get("sample.txt");
// Write to the file
try {
List<String> lines = List.of("This is the first line.", "This is the second line.", "This is the third line.");
Files.write(filePath, lines, StandardCharsets.UTF_8);
System.out.println("Successfully wrote to file.");
} catch (IOException e) {
System.err.println("An error occurred while writing to the file: " + e.getMessage());
}
// Read from the file
try {
List<String> allLines = Files.readAllLines(filePath, StandardCharsets.UTF_8);
System.out.println("Contents of the file:");
for (String line : allLines) {
System.out.println(line);
}
} catch (IOException e) {
System.err.println("An error occurred while reading from the file: " + e.getMessage());
}
}
}
Concepts Behind the Snippet
This snippet uses the `java.nio.file` package, which provides a modern approach to file handling in Java. Key concepts include:
Real-Life Use Case
This technique is commonly used for configuration files, log files, data storage, and any scenario where you need to persist or retrieve textual data to/from a file. Imagine a program that stores user preferences in a file - this code snippet showcases the core principles of how to accomplish that.
Best Practices
Interview Tip
Be prepared to discuss the advantages of the `Path` and `Files` API over older `java.io.File` class. Mention improved security, more flexible path manipulation, and better exception handling.
When to Use Them
Use the `Path` and `Files` API when you need to perform file I/O operations in Java, especially when dealing with text files, configuration files, or any scenario where you need fine-grained control over file access.
Memory Footprint
The `readAllLines` method reads the entire file into memory. For very large files, this could lead to memory issues. Consider using `Files.lines` which returns a stream for processing the file line by line, offering better memory efficiency for large files.
Alternatives
Pros
Cons
FAQ
-
What happens if the file doesn't exist when I try to read it?
An `IOException` will be thrown. You need to handle this exception in your code using a try-catch block. Alternatively, you can check if the file exists before attempting to read it using `Files.exists(filePath)`. -
How can I append to a file instead of overwriting it?
Use `Files.write(filePath, lines, StandardCharsets.UTF_8, StandardOpenOption.APPEND)` where `StandardOpenOption.APPEND` specifies that the lines should be appended to the file. -
How to create a directory with Path API?
You can use `Files.createDirectory(path)` or `Files.createDirectories(path)` to create a single directory or the whole path, respectively.