This episode covers I/O and files in Java: the basics of java.io and java.nio, reading and writing text, binary, and CSV files, the Path API and Files utility, working directory, and best practices for resource management with try-with-resources.

Almost every application interacts with files: reading configuration, writing logs, importing data, or exporting reports. Episode 8 covers input/output and file management in Java — from the classic java.io API to the modern java.nio. You will learn to read and write text, binary, and CSV files, and apply best practices for resource management.
java.io is the classic stream-based API: FileInputStream, FileOutputStream, FileReader, and FileWriter. java.nio (introduced in Java 7) offers a more modern and flexible API: Path, Files, channels, and buffers. For everyday needs, java.nio.file.Files is a concise entry point.
Read an entire text file in a single line of code:
import java.nio.file.*;
public class BacaTeks {
public static void main(String[] args) throws IOException {
String isi = Files.readString(Path.of("catatan.txt"));
System.out.println(isi);
}
}Files.readString(Path.of("catatan.txt")) reads the entire file into a single string. For large files, use readAllLines or a stream.
Use Files.writeString to write text, or Files.write for a list of lines:
import java.nio.file.*;
import java.util.List;
public class TulisTeks {
public static void main(String[] args) throws IOException {
Files.writeString(Path.of("output.txt"), "Halo Java");
Files.write(Path.of("baris.txt"), List.of("Baris 1", "Baris 2"));
}
}To process large files efficiently, read line by line:
import java.io.*;
import java.nio.file.*;
public class BacaBaris {
public static void main(String[] args) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(Path.of("log.txt"))) {
String baris;
while ((baris = reader.readLine()) != null) {
System.out.println(baris);
}
}
}
}Binary files such as images, audio, and archives cannot be read as text. Use byte streams with Files.copy to copy, or Files.readAllBytes to process:
import java.nio.file.*;
public class SalinBinary {
public static void main(String[] args) throws IOException {
Path sumber = Path.of("gambar.png");
Path tujuan = Path.of("gambar-copy.png");
Files.copy(sumber, tujuan, StandardCopyOption.REPLACE_EXISTING);
}
}Files.copy with the REPLACE_EXISTING option copies a binary file and overwrites the destination if it already exists.
CSV (Comma-Separated Values) is a text format with values separated by commas. Read line by line and split with split:
import java.io.*;
import java.nio.file.*;
public class BacaCsv {
public static void main(String[] args) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(Path.of("produk.csv"))) {
String baris;
while ((baris = reader.readLine()) != null) {
String[] kolom = baris.split(",");
System.out.println(kolom[0] + " - " + kolom[1]);
}
}
}
}Writing CSV is as simple as composing a string per line:
List<String> baris = List.of("1,Java,50000", "2,Python,45000");
Files.write(Path.of("produk.csv"), baris);For complex CSV, consider a library such as OpenCSV.
java.nio.file provides various file operations:
import java.nio.file.*;
public class OperasiFile {
public static void main(String[] args) throws IOException {
Path dir = Path.of("backup");
Files.createDirectories(dir);
Files.move(Path.of("a.txt"), Path.of("b.txt"));
Files.deleteIfExists(Path.of("sementara.txt"));
}
}The working directory is the directory where a process is run — the starting point for relative paths:
pwdRelative paths such as Path.of("data.txt") are always interpreted relative to the working directory.
The golden rule: every resource that is opened must be closed. Try-with-resources handles this automatically and is safe against exceptions:
try (BufferedReader reader = Files.newBufferedReader(Path.of("input.txt"));
BufferedWriter writer = Files.newBufferedWriter(Path.of("output.txt"))) {
String baris;
while ((baris = reader.readLine()) != null) {
writer.write(baris.toUpperCase());
writer.newLine();
}
}Episode 8 teaches I/O and file management: distinguishing java.io and java.nio, reading and writing text, binary, and CSV files, using the Path API and Files utility, understanding the working directory, and applying try-with-resources for resource management.
Key takeaways:
java.nio.file.Files is a concise entry point for modern I/O.readString or readAllLines.Files.copy or readAllBytes.split(",").In the next episode, episode 9, we will discuss data binding and JSON processing — the process of JSON serialization and deserialization in Java, the popular libraries Jackson, Gson, and JSON-B, mapping Java objects to JSON and back, and validating input data plus error handling while parsing. Time to talk to the outside world!