Java IO Tutorial | Classes, Interfaces

Java IO (Input and Output) is a process of reading data from a source and writing text data to a destination.

Data is read (or retrieved) from an input source and written to the output destination.

In other simple words, input represents data given to a program, and output represents data sent to an output destination as a result of the program.

For example, a keyboard acts as a standard input source that reads data entered using the keyboard into your program.

In the first Java program, we had used System.out.println() method to print text as an output of program on the console. Actually, we have performed I/O operations.

Other examples of input sources that input to a program, are mouse, memory, disk, network, or another program.

Similarly, other examples of output destinations that display output of a program, are screen, printer, memory, disk, network, or another program.

Java IO (Input and Output)

Input to a program can be given directly inside the program itself. In this case, every time the same input is used by the program and the same result can be displayed on the screen.

For example, consider the below code segment:

int x, y;
x = 20;
y = 30;
int z = x + y;
System.out.println(z);

In this code segment, the values of x and y are taken as 20 and 30, respectively, and the value of z is calculated and displayed on the screen. Even if this code is executed several times, we get the same value of z as 50.

Java IO Classes and Interfaces


Java language introduces a rich set of I/O classes and interfaces in the core API, mostly in the java.io package. The I/O classes defined by java.io are listed in the below table:

BufferedInputStream FileWriter PipedOutputStream
BufferedOutputStream FilterInputStream PipedReader
BufferedReader FilterOutputStream PipedWriter
BufferedWriter FilterReader PrintStream
ByteArrayInputStream FilterWriter PrintWriter
ByteArrayOutputStream InputStream PushbackInputStream
CharArrayReader InputStreamReader PushbackReader
CharArrayWriter LineNumberReader RandomAccessFile
Console ObjectInputStream Reader
DataInputStream ObjectInputStream.GetField SequenceInputStream
DataOutputStream ObjectOutputStream SerializablePermission
File ObjectOutputStream.PutField StreamTokenizer
FileDescriptor ObjectStreamClass StringReader
FileInputStream ObjectStreamField StringWriter
FileOutputStream OutputStream Writer
FilePermission OutputStreamWriter
FileReader PipedInputStream

Console was introduced by Java SE 6.

Two deprecated classes defined in java.io package that is not shown in the preceding table: LineNumberInputStream and StringBufferInputStream. These two classes are not used for new code.

The interfaces defined by java.io are listed below:

Closeable FileFilter ObjectInputValidation
DataInput FilenameFilter ObjectOutput
DataOutput Flushable ObjectStreamConstants
Externalizable ObjectInput Serializable

As you can observe in the preceding table, there are many classes and interfaces defined in the java.io package to perform input/output (io) operations.


Hope that this tutorial has covered almost all the important points concerning Java IO (Input and Output) and its classes and interfaces with examples. I hope that you will have understood the basics of Input/Output in Java.

In the next tutorial, we will learn Stream and its types in Java language.
Thanks for reading!!!
Next ⇒ Stream in Java

⇐ Prev Next ⇒