Java Tutorial

File I/O

Reading and writing files with legacy java.io and modern java.nio (NIO.2)

What is Java File I/O?

In Java, if you want to read from or write data to a file, you use the standard library's Input/Output (I/O) features. There are two "generations" of I/O in Java:
  1. Legacy I/O (java.io): Older, simpler, but can be slow.
  2. Modern I/O (java.nio): Introduced in Java 7, this is much faster and uses non-blocking logic.
java
import java.nio.file.*;
import java.io.IOException;

// Modern logic!
Path path = Paths.get("data.txt");
Files.writeString(path, "Hello, Java!");

String content = Files.readString(path);

Comparisons: Readers vs Streams

Wait! Before you read a file, you must pick the right "lane" based on what data is inside:
  • Readers/Writers: For Text files. (Handles character encoding like UTF-8 automatically).
  • InputStream/OutputStream: For Binary files (images, PDFs, or compiled code).
Stream TypeExamplesBest Use...
ReaderBufferedReaderReading large text files line-by-line.
WriterBufferedWriterWriting long text files efficiently.
InputS.FileInputStreamLoading an image or a ZIP file.
OutputS.FileOutputStreamSaving binary data back to disk.

The Modern Way: Files and Path (NIO.2)

Since Java 7, we use the Files utility class. It provides static methods that handle all the complex logic for you in one single line.
java
Path source = Paths.get("old.txt");
Path dest = Paths.get("new.txt");

// One line to copy a whole file!
Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);

Safety: The try-with-resources Statement

Wait! Every time you open a file, your Operating System creates a "handle." If you don't close it, your application will "leak" memory. Always use try-with-resources to ensure the file is closed automatically.
java
try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
  String line;
  while ((line = br.readLine()) != null) {
      System.out.println(line);
  }
} catch (IOException e) {
  System.err.println("Task failed: " + e.getMessage());
}
// File is CLOSED automatically after '}'!

Summary

TermDescription
PathModern way to represent a file location
FilesStatic utility class with high-level methods
StreamMoving 8-bit bytes (Binary data)
ReaderMoving 16-bit characters (Text data)
BufferIntermediate storage that speeds up disk access
NIO.2The modern, fast, and secure I/O API (Java 7+)
Try-ResourcesThe safest way to open and auto-close files
Best ForData logging, loading configs, saving assets
ModernAlways use java.nio.file for new projects!
EncodingFiles.readString(path, StandardCharsets.UTF_8)
CheckFiles.exists(path) (Check before you read!)
TempFiles.createTempFile("prefix", ".suffix")
WalkFiles.walk(dir) (Find files inside subfolders!)
DeleteFiles.deleteIfExists(path)
MetadataFiles.getAttribute(path, "size") (File size, etc.)
StandardEstablished and trusted worldwide for 25+ years
ChannelEven FASTER than Streams for large-scale data moves
SafetyPrevents "Resource Leaks" with auto-closing logic
EfficiencySmall buffers mean less pressure on your RAM
PermissionsCheck if a file is readable or writable!
AtomicStandardCopyOption.ATOMIC_MOVE (Safe moving!)
RecursionDeleting a folder requires deleting all children first
MemoryJava NIO can map files directly into RAM for speed
PointerRandomAccessFile (Jump anywhere in the file!)
SerializableSaving whole objects to files (See Serialization section!)
LegacyFile.exists() (Avoid this; use Path instead!)