java.io Package

Input/output classes for reading and writing files and streams.

File

Representation of file and directory pathnames.

Constructors

File(String pathname)

Creates a File instance for the specified path.

File(String parent, String child)

Creates a File from parent directory and child pathname.

Query Methods

boolean exists()

Tests whether the file or directory exists.

boolean isFile()

Tests whether this is a regular file.

boolean isDirectory()

Tests whether this is a directory.

long length()

Returns the length of the file in bytes.

Path Methods

String getName()

Returns the name of the file or directory.

String getPath()

Returns the pathname string.

String getAbsolutePath()

Returns the absolute pathname string.

String getParent()

Returns the pathname of the parent directory.

File Operations

boolean createNewFile()

Creates a new empty file if it doesn't exist.

boolean delete()

Deletes the file or directory.

boolean mkdir()

Creates the directory.

boolean mkdirs()

Creates the directory including parent directories.

Directory Listing

String[] list()

Returns an array of names of files in the directory.

File[] listFiles()

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

FileReader(String fileName)

Creates a FileReader for the specified file.

FileReader(File file)

Creates a FileReader for the specified File object.

Methods

int read()

Reads a single character. Returns -1 at end of file.

int read(char[] cbuf)

Reads characters into an array. Returns the number of characters read, or -1 at EOF.

boolean ready()

Returns true if the stream is ready to be read.

void close()

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

FileWriter(String fileName)

Creates a FileWriter for the specified file (overwrites existing).

FileWriter(String fileName, boolean append)

Creates a FileWriter with append mode option.

FileWriter(File file)

Creates a FileWriter for the specified File object.

Methods

void write(int c)

Writes a single character.

void write(String str)

Writes a string.

void write(char[] cbuf, int off, int len)

Writes a portion of a character array.

void flush()

Flushes the stream, writing any buffered data.

void close()

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

BufferedReader(Reader in)

Creates a BufferedReader wrapping another Reader (typically FileReader).

Methods

int read()

Reads a single character. Returns -1 at EOF.

int read(char[] cbuf)

Reads characters into an array.

String readLine()

Reads a line of text. Returns null at EOF.

boolean ready()

Returns true if the stream is ready.

void close()

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

PrintWriter(String fileName)

Creates a PrintWriter for the specified file.

PrintWriter(Writer out)

Creates a PrintWriter wrapping another Writer.

Methods

void print(String s)
void print(int i)
void print(Object obj)

Prints without a newline.

void println(String s)
void println(int i)
void println(Object obj)
void println()

Prints with a newline.

PrintWriter printf(String format, Object... args)

Writes a formatted string.

void flush()

Flushes the stream.

void close()

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.

String getMessage()

Returns the detail message.

try {
    FileReader reader = new FileReader("nonexistent.txt");
} catch (IOException e) {
    System.out.println("File not found: " + e.getMessage());
}