Read & write file in java (FileInputStream/ FileOutputStream/ example)

  • Create or write file in java using FileOutputStream class.
    • FileOutputStream writes the contents to file as stream of bytes.
    • The FileOutputStream class has following write methods:
      • public void write(int b)
      • public void write(byte[] b)
      • public void write(byte[] b, int off, int len)
    • FileOutputStream class hierarchy java
      File 1: FileOutputStream class hierarchy
  • Read contents of file in java using FileInputStream class.
    • FileInputStream reads the contents of file as stream of bytes.
    • The read methods of FileInputStream are.
      • public int read()
      • public int read(byte[] b)
      • public int read(byte[] b, int off, int len)
FileInputStream java class hierarchy
File 2: FileInputStream class hierarchy

1. Program: read & write file in java (FileInputStream/FileOutputStream)

package org.learn.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileInputOutputStream {

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

        //Write contents to file in java using FileOutputStream
        writeContentsUsingFileOutputStream();

        //Read contents of file in java using FileInputStream
        readContentsUsingFileInputStream();
    }

    private static void writeContentsUsingFileOutputStream() throws IOException {
        String newLine = System.getProperty("line.separator");
        try (FileOutputStream fileStream = new FileOutputStream(new File("sampleFile.txt"))) {
            System.out.println("1. Start Writing file using FileOutputStream:");
            fileStream.write("Soccer".getBytes());
            System.out.println("Written Soccer to a file");
            fileStream.write(newLine.getBytes());
            fileStream.write("Tennis".getBytes());
            System.out.println("Written Tennis to a file");
            fileStream.write(newLine.getBytes());
            fileStream.write("Badminton".getBytes());
            System.out.println("Written Badminton to a file");
            fileStream.write(newLine.getBytes());
            fileStream.write("Hockey".getBytes());
            System.out.println("Written Hockey to a file");
            fileStream.write(newLine.getBytes());
            System.out.println("2. End Writing file using FileOutputStream");
            System.out.println();
        }
    }

    private static void readContentsUsingFileInputStream() throws IOException {
        try (FileInputStream fileStream = new FileInputStream(new File("sampleFile.txt"))) {
            System.out.println("3. Start Reading file using FileInputStream:");
            int character;
            while ((character = fileStream.read()) != -1) {
                System.out.print((char) character);
            }
            System.out.println("4. End Reading file using FileInputStream");
        }
    }
}

The contents of sampleFile.txt after write operation is as follows:

Read and write file in java
Fig 3: Contents written to a file

2. Output: read & write file in java (FileInputStream/FileOutputStream)

1. Start Writing file using FileOutputStream:
Written Soccer to a file
Written Tennis to a file
Written Badminton to a file
Written Hockey to a file
2. End Writing file using FileOutputStream:

3. Start Reading file using FileInputStream:
Soccer
Tennis
Badminton
Hockey
4. End Reading file using FileInputStream
Scroll to Top