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.
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.
The java.io package contains several classes and interfaces for performing input and output operations. Some commonly used classes and interfaces are discussed below.
| Class | Description |
|---|---|
| BufferedInputStream | The 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. |
| BufferedOutputStream | The 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. |
| BufferedReader | The 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. |
| BufferedWriter | The 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. |
| ByteArrayInputStream | The 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. |
| ByteArrayOutputStream | The 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. |
| CharArrayReader | The 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. |
| CharArrayWriter | The 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. |
| Console | All 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. |
| DataInputStream | The 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 |
| DataOutputStream | The 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 |
| File | The 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 |
| FileDescriptor | It 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 |
| FileInputStream | The 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 |
| FileOutputStream | It 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 |
| FilePermission | All 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 |
| FileReader | The 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 |
| FileWriter | The 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 |
| FilterInputStream | The 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. |
| FilterOutputStream | The 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. |
| FilterReader | The 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. |
| FilterWriter | The 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. |
| InputStream | The 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. |
| InputStreamReader | The 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. |
| LineNumberInputStream | The 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. |
| LineNumberReader | It keeps track of the line numbers. The class is declared as: public class LineNumberReader extends BufferedReader The class extends theBufferedReaderclass. |
| ObjectInputStream | The 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.GetField | The 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. |
| ObjectOutputStream | Objects 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.PutField | There 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. |
| ObjectStreamClass | It 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. |
| ObjectStreamField | There 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. |
| OutputStream | The 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. |
| OutputStreamWriter | The 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. |
| PipedInputStream | It is used for reading data from the piped input streams. The class is declared as: public class PipedInputStream extends InputStream The class extends theInputStreamclass. |
| PipedOutputStream | The 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. |
| PipedReader | The 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. |
| PipedWriter | The 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. |
| PrintStream | The 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. |
| PrintWriter | The 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. |
| PushbackInputStream | It 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. |
| PushbackReader | It is used to unread a char value. The class is declared as: public class PushbackReader extends FilterReader The class extends theFilterReader class. |
| RandomAccessFile | It 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. |
| Reader | The 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. |
| SequenceInputStream | The 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. |
| SerializablePermission | The 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 |
| StreamTokenizer | The 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. |
| StringBufferInputStream | It was originally designed for conversion of characters into bytes. The class is declared as: public class StringBufferInputStream extends InputStream The class extends theInputStream class. |
| StringReader | The 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. |
| StringWriter | The 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. |
| Writer | The 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. |
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.
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.
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.
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
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.
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.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India