Provides classes for system input/output through data streams, serialization, and the file system. For modern code prefer java.nio.file (Files, Path) for file operations, but java.io streams remain essential for all I/O.
Import: import java.io.*; | Docs: Oracle JavaDoc
| Name | Signature | Description |
|---|---|---|
| File | class File | Abstract representation of file/directory path |
| file.exists | boolean exists() | True if file/directory exists |
| file.isFile | boolean isFile() | True if path is a regular file |
| file.isDirectory | boolean isDirectory() | True if path is a directory |
| file.length | long length() | Return file size in bytes |
| file.listFiles | File[] listFiles() | Return array of files in directory |
| file.mkdir | boolean mkdir() | Create directory |
| file.delete | boolean delete() | Delete file or empty directory |
| FileReader | class FileReader extends InputStreamReader | Read text file character by character |
| FileWriter | class FileWriter extends OutputStreamWriter | Write text to file |
| BufferedReader | class BufferedReader | Buffered character reading — readLine() |
| br.readLine | String readLine() throws IOException | Read one line; null at EOF |
| BufferedWriter | class BufferedWriter | Buffered character writing |
| bw.write | void write(String s) throws IOException | Write string to buffer |
| bw.newLine | void newLine() throws IOException | Write platform-specific line separator |
| bw.flush | void flush() throws IOException | Flush buffer to underlying stream |
| PrintWriter | class PrintWriter | Convenient writer with print/println/printf |
| FileInputStream | class FileInputStream | Read raw bytes from file |
| FileOutputStream | class FileOutputStream | Write raw bytes to file |
| ByteArrayOutputStream | class ByteArrayOutputStream | Write bytes to in-memory buffer |
| Files.readAllBytes | static byte[] readAllBytes(Path p) | Read entire file as bytes (nio) |
| Files.readString | static String readString(Path p) | Read entire file as String (Java 11+, nio) |
| Files.writeString | static Path writeString(Path p, CharSequence cs) | Write String to file (Java 11+, nio) |
| Files.lines | static Stream<String> lines(Path p) | Stream lines of file (nio, Java 8+) |
| Path.of | static Path of(String first, String... more) | Create Path from string (nio, Java 11+) |
| IOException | class IOException extends Exception | Checked exception for I/O errors |
Save as ClassName.java (matching the public class name), compile with javac ClassName.java, run with java ClassName.
import java.io.*;
import java.nio.file.*;
public class JavaIODemo {
public static void main(String[] args) throws IOException {
// --- Write with PrintWriter (try-with-resources) ---
Path file = Path.of("students.txt");
try (PrintWriter pw = new PrintWriter(Files.newBufferedWriter(file))) {
pw.println("Alice 95");
pw.println("Bob 87");
pw.println("Carol 92");
pw.println("Dave 78");
}
// --- Read with BufferedReader ---
System.out.println("=== BufferedReader ===");
try (BufferedReader br = Files.newBufferedReader(file)) {
String line;
int lineNum = 0;
while ((line = br.readLine()) != null)
System.out.printf(" %d: %s%n", ++lineNum, line);
}
// --- nio: Files.readString (Java 11+) ---
String content = Files.readString(file);
System.out.println("=== readString length: " + content.length() + " chars ===");
// --- nio: Files.lines stream ---
System.out.println("=== Lines with score > 90 ===");
Files.lines(file)
.filter(l -> {
String[] p = l.trim().split("\\s+");
return p.length > 1 && Integer.parseInt(p[1]) > 90;
})
.forEach(l -> System.out.println(" " + l));
// --- File metadata ---
File f = file.toFile();
System.out.printf("Size: %d bytes exists=%b%n", f.length(), f.exists());
Files.delete(file);
System.out.println("File deleted.");
}
}
// javac JavaIODemo.java && java JavaIODemo