Open In App

Input/Output in Java with Examples

Last Updated : 04 Sep, 2025
Comments
Improve
Suggest changes
50 Likes
Like
Report

Java I/O (Input/Output) is a collection of classes and streams in the java.io package that handle reading data from sources (like files, keyboard, or network) and writing data to destinations (like files, console or sockets). It provides both byte and character streams to support all types of data.

java_application
Flow from Source to Destination

Default/Standard Streams in Java

Before exploring various input and output streams, let's look at 3 most commonly used default streams that Java has provided:

standard_i_o_streams_in_java
Default Stream
  • System.in: This is the standard input stream that is used to read characters from the keyboard or any other standard input device.
  • System.out: This is the standard output stream that is used to produce the result of a program on an output device like the computer screen.
  • System.err: This is the standard error stream that is used to display error messages separately from normal output.

Functions used with Default Streams:

1. System.in

System.in is an InputStream, so it provides methods from the InputStream class. Commonly used ones are:

  • int read(): reads one byte of data.
  • int read(byte[] b): reads bytes into an array.
  • int read(byte[] b, int off, int len): reads bytes into part of an array.
  • void close(): closes the input stream.
  • int available(): returns the number of bytes that can be read without blocking.

Usually, System.in is wrapped with classes like Scanner or BufferedReader for easier input handling.

Example:

Output:

Screenshot-2025-08-27-122147
Output

2. System.out

print(): This Java method displays text on the console, keeping the cursor at the end of the printed text so the next output continues from the same line.

Syntax:

System.out.print(parameter);

Example:


Output
GfG! GfG! GfG! 

println(): This method in Java is also used to display a text on the console. It prints the text on the console and the cursor moves to the start of the next line at the console. The next printing takes place from the next line.

Syntax:

System.out.println(parameter);

Example:


Output
GfG! 
GfG! 
GfG! 

printf(): The printf() method in Java is used for formatted output and can take multiple arguments, making it more flexible than print() or println().

Example:


Output
Printing simple integer: x = 100
Formatted with precision: PI = 3.14
Formatted to specific width: n = 5.2000
Formatted to right margin: n =         2324435.2500

3. System.err

It is used to display the error messages. It works similarly to System.out with print(), println(), and printf() methods.

Example: Java Program demonstrating System.err

public class Geeks {
  
    public static void main(String[] args) {
      
        // Using print()
        System.err.print("This is an error message using print().\n");

        // Using println()
        System.err.println("This is another error message using println().");

        //Using printf()
        System.err.printf("Error code: %d, Message: %s%n", 404, "Not Found");
    }
}

Output:

Screenshot-2025-08-27-122306
Output

Types of Streams

Depending on the type of operations, streams can be divided into two primary classes:

1. Input Stream: These streams are used to read data that must be taken as an input from a source array or file or any peripheral device. For eg., FileInputStream, BufferedInputStream, ByteArrayInputStream etc.

various_inputstream_classes
InputStream

2. Output Stream: These streams are used to write data as outputs into an array or file or any output peripheral device. For eg., FileOutputStream, BufferedOutputStream, ByteArrayOutputStream etc.

various_outputstream_classes
Output Stream

To know more about types of Streams, you can refer to: Java.io.InputStream Class & Java.io.OutputStream class.

Types of Streams Depending on the Types of File

Depending on the types of file, Streams can be divided into two primary classes which can be further divided into other classes as can be seen through the diagram below followed by the explanations.

stream_classifications_based_on_file_types
Classification

1. ByteStream:

Byte streams in Java are used to perform input and output of 8-bit bytes. They are suitable for handling raw binary data such as images, audio, and video, using classes like InputStream and OutputStream.

Here is the list of various ByteStream Classes:

Stream classDescription
BufferedInputStreamUsed to read data more efficiently with buffering.
DataInputStreamProvides methods to read Java primitive data types.
FileInputStreamThis is used to read from a file.
InputStreamThis is an abstract class that describes stream input.
PrintStreamThis contains the most used print() and println() method
BufferedOutputStreamThis is used for Buffered Output Stream.
DataOutputStreamThis contains method for writing java standard data types.
FileOutputStreamThis is used to write to a file.
OutputStreamThis is an abstract class that describes stream output.

Example: Java Program illustrating the Byte Stream to copy contents of one file to another file.

import java.io.*;
public class Geeks {
    public static void main(
        String[] args) throws IOException
    {

        FileInputStream sourceStream = null;
        FileOutputStream targetStream = null;

        try {
            sourceStream
                = new FileInputStream("sorcefile.txt");
            targetStream
                = new FileOutputStream("targetfile.txt");

            // Reading source file and writing content to target file byte by byte
            int temp;
            while ((
                       temp = sourceStream.read())
                   != -1)
                targetStream.write((byte)temp);
        }
        finally {
            if (sourceStream != null)
                sourceStream.close();
            if (targetStream != null)
                targetStream.close();
        }
    }
}

Output:

Shows contents of file test.txt

2. CharacterStream:

Character streams in Java are used to perform input and output of 16-bit Unicode characters. They are best suited for handling text data, using classes like Reader and Writer which automatically handle character encoding and decoding.

Here is the list of various CharacterStream Classes:

Stream classDescription
BufferedReaderIt is used to handle buffered input stream.
FileReaderThis is an input stream that reads from file.
InputStreamReaderThis input stream is used to translate byte to character.
OutputStreamWriterConverts character stream to byte stream.
ReaderThis is an abstract class that define character stream input.
PrintWriterThis contains the most used print() and println() method
WriterThis is an abstract class that define character stream output.
BufferedWriterThis is used to handle buffered output stream.
FileWriterThis is used to output stream that writes to file.

Example:

import java.io.*;

public class Geeks 
{
    public static void main(String[] args) throws IOException
    {
        FileReader sourceStream = null;
        
      	try {
            sourceStream = new FileReader("test.txt");

            // Reading sourcefile and writing content to target file character by character.
            int temp;
          
            while (( temp = sourceStream.read())!= -1 )
                System.out.println((char)temp);
        }
        finally {
          
            // Closing stream as no longer in use
            if (sourceStream != null)
                sourceStream.close();
        }
    }
}

Output in Java
Visit Course explore course icon

Explore