java.io Package
Input/output classes for reading and writing files and streams.
File
Representation of file and directory pathnames.
Constructors
Creates a File instance for the specified path.
Creates a File from parent directory and child pathname.
Query Methods
Tests whether the file or directory exists.
Tests whether this is a regular file.
Tests whether this is a directory.
Returns the length of the file in bytes.
Path Methods
Returns the name of the file or directory.
Returns the pathname string.
Returns the absolute pathname string.
Returns the pathname of the parent directory.
File Operations
Creates a new empty file if it doesn't exist.
Deletes the file or directory.
Creates the directory.
Creates the directory including parent directories.
Directory Listing
Returns an array of names of files in the directory.
Returns an array of File objects for files in the directory.
File file = new File("/path/to/file.txt");
if (file.exists()) {
System.out.println("Size: " + file.length());
System.out.println("Name: " + file.getName());
}
File dir = new File("/path/to/dir");
if (dir.isDirectory()) {
for (String name : dir.list()) {
System.out.println(name);
}
}
FileReader
Convenience class for reading character files.
Constructors
Creates a FileReader for the specified file.
Creates a FileReader for the specified File object.
Methods
Reads a single character. Returns -1 at end of file.
Reads characters into an array. Returns the number of characters read, or -1 at EOF.
Returns true if the stream is ready to be read.
Closes the stream and releases resources.
FileReader reader = new FileReader("input.txt");
int ch;
while ((ch = reader.read()) != -1) {
System.out.print((char) ch);
}
reader.close();
FileWriter
Convenience class for writing character files.
Constructors
Creates a FileWriter for the specified file (overwrites existing).
Creates a FileWriter with append mode option.
Creates a FileWriter for the specified File object.
Methods
Writes a single character.
Writes a string.
Writes a portion of a character array.
Flushes the stream, writing any buffered data.
Closes the stream.
FileWriter writer = new FileWriter("output.txt");
writer.write("Hello, World!\n");
writer.write("Second line");
writer.close();
// Append mode
FileWriter appender = new FileWriter("output.txt", true);
appender.write("\nAppended text");
appender.close();
BufferedReader
Reads text from a character stream with buffering for efficiency.
Constructor
Creates a BufferedReader wrapping another Reader (typically FileReader).
Methods
Reads a single character. Returns -1 at EOF.
Reads characters into an array.
Reads a line of text. Returns null at EOF.
Returns true if the stream is ready.
Closes the stream.
BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
PrintWriter
Prints formatted representations of objects to a text output stream.
Constructors
Creates a PrintWriter for the specified file.
Creates a PrintWriter wrapping another Writer.
Methods
Prints without a newline.
Prints with a newline.
Writes a formatted string.
Flushes the stream.
Closes the stream.
PrintWriter writer = new PrintWriter("output.txt");
writer.println("Name: Alice");
writer.printf("Age: %d%n", 25);
writer.close();
Try-with-Resources
All I/O classes implement AutoCloseable and should be used with try-with-resources:
try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
PrintWriter writer = new PrintWriter("output.txt")) {
String line;
while ((line = reader.readLine()) != null) {
writer.println(line.toUpperCase());
}
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
// Resources automatically closed
IOException
Signals that an I/O exception has occurred.
Returns the detail message.
try {
FileReader reader = new FileReader("nonexistent.txt");
} catch (IOException e) {
System.out.println("File not found: " + e.getMessage());
}