java.io Package

Last Updated : 6 Oct 2026

The java.io package provides classes and interfaces for reading data from various sources and writing data to different destinations. It includes classes for file handling, input and output streams, readers, writers, and other input/output operations.

In this chapter, you will learn about the java.io package and the commonly used classes and interfaces provided for input and output operations.

What is java.io Package?

The java.io package is a standard Java package that provides classes and interfaces for performing input and output operations. It can be used to read data from sources such as files and other input streams and to write data to files and other output streams.

The package contains classes for working with byte streams, character streams, files, buffered input and output, object serialization, and other I/O operations.

Classes and Interfaces in java.io Package

The java.io package contains several classes and interfaces for performing input and output operations. Some commonly used classes and interfaces are discussed below.

ClassDescription
BufferedInputStreamThe class is responsible for adding the functionality to other input streams. The class is declared as:public class BufferedInputStream extends FilterInputStream
The class extends the FilterInputStream class.
BufferedOutputStreamThe class is responsible for the implementation of Buffered output stream. The class is declared as:
public class BufferedOutputStream
extends FilterOutputStream
The class extends the FilterInputStream class.
BufferedReaderThe class reads the texts from the char input stream. The class is declared as:
public class BufferedReader
extends Reader
The class extends the Reader class.
BufferedWriterThe class write the texts to the char input stream. The class is declared as:
public class BufferedWriter
extends Writer
The class extends the Writer class.
ByteArrayInputStreamThe class is responsible for reading of data form byte array. The class is declared as:
public class ByteArrayInputStream
extends InputStream
The class extends the InputStreamclass.
ByteArrayOutputStreamThe class is responsible for writing of data into byte array. The class is declared as:
public class ByteArrayOutputStream
extends OutputStream
The class extends the OutputStream class.
CharArrayReaderThe class is responsible for the implementation of character buffer to be further used as character input stream. The class is declared as:
public class CharArrayReader extends Reader
The class extends the Reader class.
CharArrayWriterThe class is responsible for the implementation of character buffer to be further used as character output stream. The class is declared as:
public class CharOutputWriter extends Writer
The class extends the Writer class.
ConsoleAll theassociated console devices can be accessed using this class. The class is declared as:
public final class Console extends Object
implements Flushable
The class extends the Object class.
DataInputStreamThe stream is used for reading the java data types which are primitive. The class is declared as:
public class DataInputStream extends FilterInputStream implements DataInputThe class extends the FilterInputStreamclass
DataOutputStreamThe stream is used for writing the java data types which are primitive. The class is declared as:
public class DataOutputStream extends FilterOutputStream implements DataOutput
The class extends the FilterOutputStream class
FileThe class provides a representation of the file path names. The class is declared as:
public class File extends Object implements Serializable, Comparable<File>
The class extends the Object class
FileDescriptorIt is mainly used for creation of file input and output streams. The class is declared as:
public final class FileDescriptor
extends Object
he class extends the Object class
FileInputStreamThe class is used to retrieve the input bytes from a file system. The class is declared as:
public class FileInputStream extends InputStream
The class extends the InputStream class
FileOutputStreamIt is a parent class of all the output stream classes. The class is declared as:
public class FileOutputStream extends OutputStream
The class extends the OutputStream class
FilePermissionAll the access related issues are handled by the class. The class is declared as:
public final class FilePermission extends Permission
implements Serializable
The class extends the Permisison class
FileReaderThe class is used for reading data from the character files. The class is declared as:
public class FileReader extends InputStreamReader
The class extends the InputStreamReaderclass
FileWriterThe class is used for writing data into the character files. The class is declared as:
public class FileWriter extends OutputStreamWriter
The class extends the OutputStreamWriter class
FilterInputStreamThe class is a parent class of all the ones which filter the input streams. The class is declared as:
public class FilterInputStream
extends InputStream
The class extends the InputStreamclass.
FilterOutputStreamThe class is a parent class of all the ones which filter the output streams. The class is declared as:
public class FilterOutputStream
extends OutputStream
The class extends the OutputStream class.
FilterReaderThe class is an abstract class which is responsible for reading from the filtered char streams. The class is declared as:
public abstract class FilterReader extends Reader
The class extends the Reader class.
FilterWriterThe class is an abstract class which is responsible for writing into the filtered char streams. The class is declared as:
public abstract class FilterWriter extends Writer
The class extends the Writer class.
InputStreamThe class is the parent class of all the input stream classes. The class is declared as:
public abstract class InputStream extends Object
implements Closeable
The class extends the Object class.
InputStreamReaderThe class reads from the byte stream files and decodes it into the char stream. The class is declared as:
public class InputStreamReader extends Reader
The class extends the Reader class.
LineNumberInputStreamThe class is based on the assumption that bytes represent characters. The class is declared as:
public class LineNumberInputStream
extends FilterInputStream
The class extends theFilterInputStream class.
LineNumberReaderIt keeps track of the line numbers. The class is declared as:
public class LineNumberReader extends BufferedReader
The class extends theBufferedReaderclass.
ObjectInputStreamThe data or objects which were written using the ObjectInputStream is deserialized by the class. The class is declared as:
public class ObjectInputStream extends InputStream implements ObjectInput, ObjectStreamConstants
The class extends theInputStream class.
ObjectInputStream.GetFieldThe fields which have been successfully read from the various input streams, access to them is provided by the class. The class is declared as:
public abstract static class ObjectInputStream.GetField extends Object
The class extends theObjectclass.
ObjectOutputStreamObjects and data types are written to the output stream using this class. The class is declared as:
public class ObjectOutputStream extends OutputStream
implements ObjectOutput, ObjectStreamConstants
The class extends theOutputStreamclass.
ObjectOutputStream.PutFieldThere are fields which are written to the output stream, class provides access to them. The class is declared as:
public abstract static class ObjectOutputStream.PutField
extends Object The class extends theObjectclass.
ObjectStreamClassIt is the class which is responsible for serializing the classes. The class is declared as:
public class ObjectStreamClass
extends Object
implements Serializable
The class extends theObjectclass.
ObjectStreamFieldThere are fields which are to be written in the object output, the class provides access to them. The class is declared as:
public abstract static class ObjectOutputStream.PutField
extends Object
The class extends theObjectclass.
OutputStreamThe class is the parents of all the output stream classes. The class is declared as:
public abstract class OutputStream
extends Object
implements Closeable, Flushable
The class extends theObjectclass.
OutputStreamWriterThe class is responsible for interchanging of components written in char stream to the byte stream. The class is declared as:
public class OutputStreamWriter
extends Writer
The class extends theWriterclass.
PipedInputStreamIt is used for reading data from the piped input streams. The class is declared as:
public class PipedInputStream
extends InputStream
The class extends theInputStreamclass.
PipedOutputStreamThe class is used for writing the data into piped output streams. The class is declared as:
public class PipedOutputStream
extends OutputStream
The class extends theOutputStream class.
PipedReaderThe class acts as an input stream for reading the piped characters. The class is declared as:
public class PipedReader
extends Reader
The class extends theReader class.
PipedWriterThe class is used for writing characters into the piped output. The class is declared as:
public class PrintWriter
extends Writer
The class extends theWriter class.
PrintStreamThe class is used for reading the output stream files. The class is declared as:
public class PrintStream extends FilterOutputStream implements Appendable, Closeable
The class extends theFilterOutputStream class.
PrintWriterThe class is used for printing of objects into outputs in text form. The class is declared as:
public class PrintWriter
extends Writer
The class extends theWriter class.
PushbackInputStreamIt is used to add additional properties to the input stream. It can unread a byte value. The class is declared as:
public class PushbackInputStream extends FilterInputStream
The class extends theFilterInputStream class.
PushbackReaderIt is used to unread a char value. The class is declared as:
public class PushbackReader extends FilterReader
The class extends theFilterReader class.
RandomAccessFileIt is used to access the file in consideration randomly. The class is declared as:
public class RandomAccessFile extends Object
implements DataOutput, DataInput, Closeable
The class extends theObject class.
ReaderThe class is used for reading data from the character streams. The class is declared as:
public abstract class Reader extends Object
implements Readable, Closeable
The class extends theObject class.
SequenceInputStreamThe concatenation of various input streams, logical is reflected by this class. The class is declared as:
public class SequenceInputStream
extends InputStream
The class extends theInputStream class.
SerializablePermissionThe class is used for providing permissions related to serializing. The class is declared as:
public final class SerializablePermission
extends BasicPermission
The class extends theBasicPermission class
StreamTokenizerThe class is used for reading the input streams in form of tokens. The class is declared as:
public class StreamTokenizer
extends Object
The class extends the Object class.
StringBufferInputStreamIt was originally designed for conversion of characters into bytes. The class is declared as:
public class StringBufferInputStream
extends InputStream
The class extends theInputStream class.
StringReaderThe class is used for reading the strings from the file. The class is declared as:
public class StringReader
extends Reader
The class extends theReader class.
StringWriterThe class is used for writing strings to the file. The class is declared as:
public class StringWriter extends Writer
he class extends the Writer class.
WriterThe class is used for writing characters to the output stream. The class is declared as:
public abstract class Writer
extends Object
implements Appendable, Closeable, Flushable
The class extends the Object class.

Byte Streams and Character Streams

The java.io package provides two main types of streams for handling data: byte streams and character streams. Byte streams are used to read and write raw byte data, while character streams are used to read and write text data.

InputStream and OutputStream are the main base classes for byte streams. Reader and Writer are the main base classes for character streams.

Example

In the following example, FileOutputStream writes byte data to a file, while FileWriter writes character data to another file.

Java

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

public class StreamExample {
    public static void main(String[] args) throws IOException {

        FileOutputStream byteStream = new FileOutputStream("byte.txt");
        byteStream.write(65);
        byteStream.close();

        FileWriter characterStream = new FileWriter("character.txt");
        characterStream.write("Hello Java");
        characterStream.close();

        System.out.println("Data written successfully.");
    }
}

Output:

Data written successfully.

File Handling Using java.io

The java.io package provides classes for working with files and directories. The File class can be used to check whether a file or directory exists, obtain file information, create directories, and perform operations such as renaming and deleting files.

Classes such as FileInputStream, FileOutputStream, FileReader, and FileWriter can also be used to read from and write to files.

Example

In the following example, the File class is used to check whether a file exists and display its name and path.

Java

import java.io.File;

public class FileExample {
    public static void main(String[] args) {
        File file = new File("example.txt");

        System.out.println("File Name: " + file.getName());
        System.out.println("File Path: " + file.getAbsolutePath());
        System.out.println("File Exists: " + file.exists());
    }
}

Output:

File Name: example.txt
File Path: C:\Java\example.txt
File Exists: false

Exception Handling in java.io

Input and output operations can cause exceptions such as IOException. Java provides exception handling mechanisms to handle errors that may occur while reading, writing, or closing I/O resources.

The try-catch statement can be used to handle I/O exceptions. The try-with-resources statement can also be used to automatically close resources after they are used.

Example

In the following example, FileReader is used to read a file, and the IOException is handled using a try-catch block.

Java

import java.io.FileReader;
import java.io.IOException;

public class IOExceptionExample {
    public static void main(String[] args) {

        try {
            FileReader reader = new FileReader("example.txt");
            int data = reader.read();

            System.out.println("Character: " + (char) data);

            reader.close();
        } catch (IOException e) {
            System.out.println("An I/O error occurred.");
        }
    }
}

Output:

An I/O error occurred.