Java Files
Move data across the program boundary.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind Files.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
Files are external resources and their operations can fail. The java.nio.file API works with Path values and provides convenient text methods. This example writes a temporary file in the execution environment, reads it, and removes it; it does not write a file on the reader computer.
Explore the details
File handling is an important part of any application.
Java has several methods for creating, reading, updating, and deleting files.
Java File Handling
The File class from the java.io package, allows
us to work with files.
To use the File class, create an object of
the class, and specify the filename or directory name:
If you don't know what a package is, read our Java Packages Tutorial.
The File class has many useful methods for creating and getting information
about files.
For example:
Modern File API — java.nio.file.Files
Since Java 7, java.nio.file.Files provides a cleaner, more powerful file API with less boilerplate than the old java.io approach.
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
// Write to a file (creates or overwrites)
Path file = Path.of("notes.txt");
Files.writeString(file, "Hello from NIO!");
// Read entire file as a String
String content = Files.readString(file);
System.out.println(content); // Hello from NIO!
// Read all lines as a List
List<String> lines = Files.readAllLines(file);
lines.forEach(System.out::println);
// Check if file exists
if (Files.exists(file)) {
System.out.println("File size: " + Files.size(file) + " bytes");
}
// Delete the file
Files.deleteIfExists(file);| Approach | Package | Best for | Complexity |
|---|---|---|---|
Files.writeString() / readString() | java.nio.file | Quick file read/write (Java 11+) | Low |
FileWriter / BufferedReader | java.io | Fine-grained streaming, append mode | Medium |
Scanner | java.util | Reading line-by-line interactively | Low |
Files.readAllLines() | java.nio.file | Reading all lines into a List (Java 7+) | Low |
You will learn how to create, write, read and delete files in the next chapters:
A complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) throws Exception {
Path file = Files.createTempFile("java-practice-", ".txt");
try {
Files.writeString(file, "Learning Java");
System.out.println(Files.readString(file));
} finally {
Files.deleteIfExists(file);
}
}
}Predict first. Then reveal the expected output / வெளியீடு
Learning Java
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Change the text being written and confirm the same text is read back.
Reveal the answer and reasoning / விடை
writeString stores the supplied text and readString retrieves it. The finally block attempts cleanup even if reading fails.
One mistake to avoid: Do not assume a relative path points where you expect. Its meaning depends on the process working directory.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.