Java FileInputStream
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java FileInputStream

Harine

Java FileInputStream

Introduction to FileInputStream in Java

In earlier sections, you learned how to read text files using the Scanner class. While Scanner is convenient for parsing text into lines, words, or numbers, it is primarily designed for text-based input.

However, some applications require low-level access to raw bytes, especially when working with binary files such as images, audio, or PDF documents. In such cases, Java developers use the FileInputStream class.

FileInputStream is a byte stream that reads data directly from a file one byte at a time. This makes it suitable for handling any file type, including both text and binary formats.

Reading a File Using FileInputStream

The following example demonstrates how to read a text file byte by byte and display its contents on the console.

import java.io.FileInputStream;

import java.io.IOException;

public class ByteFileReader {

  public static void main(String[] args) {

    try (FileInputStream inputStream = new FileInputStream("message.txt")) {

       int dataByte;

       while ((dataByte = inputStream.read()) != -1) {

         System.out.print((char) dataByte);

       }

    } catch (IOException error) {

       System.out.println("Unable to read the file.");

       error.printStackTrace();

      }

    }

  }

Sample Output

Welcome to Java file handling.

Learning byte streams step by step.

How the Program Works

  1. The program imports the FileInputStream class to read data from a file.
  2. A FileInputStream object opens the file message.txt.
  3. The read() method reads one byte at a time from the file.
  4. The loop continues until the end of the file is reached (-1 indicates no more data).
  5. Each byte is converted to a character and printed to the console.
  6. The try-with-resources statement ensures the stream is automatically closed.

Real-World Example: Copying a Binary File

One of the most common uses of FileInputStream is copying binary files such as images or documents.

The following program copies an image file from one location to another.

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

public class BinaryFileCopy {

  public static void main(String[] args) {

    try (FileInputStream source = new FileInputStream("photo.png");

        FileOutputStream destination = new FileOutputStream("photo_backup.png")) {

        int byteData;

        while ((byteData = source.read()) != -1) {

             destination.write(byteData);

        }

        System.out.println("File copied successfully.");

      } catch (IOException error) {

          System.out.println("File operation failed.");

          error.printStackTrace();

        }

      }

    }

Explanation

In this example:

  • The program reads bytes from photo.png using FileInpautStream.
  • Each byte is written to a new file photo_backup.png using FileOutputStream.
  • Since the program processes raw binary data, it can copy any type of file including images, videos, and documents.

Our website uses cookies to enhance your experience. Learn More
Accept !