# Working with Files in Java

In 
Published 2024-01-07

This tutorial explains how we can work with files in Java (Read / Write files).

# FileOutputStream & FileInputStream

  • FileInputStream: read bytes from files
  • FileOutputStream: write bytes to files
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

@SpringBootApplication
public class Demo1Application {

    public static void main(String[] args) throws IOException, InterruptedException {
        SpringApplication.run(Demo1Application.class, args);

        byte fileContentArray[];

        // Create a FileInputStream object
        try (FileInputStream fileInputStream = new FileInputStream("D:\\test-java\\my-dir\\text1.txt")) {

            // Create an array which has the length of the file
            fileContentArray = new byte[fileInputStream.available()];

            // You can read an array from the FileInputStream or just a byte.
            fileInputStream.read(fileContentArray);

            String fileContentString = new String(fileContentArray);

            System.out.println("data= \n-------\n"+ fileContentString);
        }

        // Create a FileOutputStream object
        try (FileOutputStream fileOutputStream = new FileOutputStream("D:\\test-java\\my-dir\\text2.txt")) {
            fileOutputStream.write(fileContentArray);
        }
	}
}

# BufferedInputStream & BufferedOutputStream

Are used for the same purpose as FileOutputStream & FileInputStream, but a buffer is created between the Java application and the stream/resource.

Take a look at the following example:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.*;

@SpringBootApplication
public class Demo1Application {

    public static void main(String[] args) throws IOException, InterruptedException {
        SpringApplication.run(Demo1Application.class, args);

        byte fileContentArray[];

        // Create a FileInputStream object
        try (FileInputStream fileInputStream = new FileInputStream("D:\\test-java\\my-dir\\text1.txt")) {

            // Create the buffer object which stay between the Java application and the stream/resource
            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

            // Create an array which has the length of the file
            fileContentArray = new byte[fileInputStream.available()];

            // You can read an array from the buffer or just a byte.
            bufferedInputStream.read(fileContentArray);

            // If we work with a text file we can print it
            String fileContentString = new String(fileContentArray);
            System.out.println("data= \n-------\n"+ fileContentString);
        }

        // Create a FileOutputStream object
        try (FileOutputStream fileOutputStream = new FileOutputStream("D:\\test-java\\my-dir\\text2.txt")) {
            // Create the buffer object which stay between the Java application and the stream/resource
            BufferedOutputStream bufferedInputStream = new BufferedOutputStream(fileOutputStream);

            // Write the content to the buffer
            bufferedInputStream.write(fileContentArray);

            // Send a message to write all buffer content to the stream/file.
            bufferedInputStream.flush();
        }
	}
}

# BufferedReader vs BufferedWriter

Here we have an example:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.*;

@SpringBootApplication
public class Demo1Application {

    public static void main(String[] args) throws IOException {
        SpringApplication.run(Demo1Application.class, args);

        FileReader fr = new FileReader("D:\\test-java\\my-dir\\text1.txt");
        FileWriter fw = new FileWriter("D:\\test-java\\my-dir\\text2.txt");
        String finalString = "";

        // BufferedReader
        try(BufferedReader br = new BufferedReader(fr)) {

            String lineRead;
            // br.ready() = true if the buffer has values inside
            while (br.ready()) {

                // once the line is read, this line is no longer in the buffer
                lineRead = br.readLine();
                finalString = finalString + lineRead + "\n";

                System.out.println("lineRead = " + lineRead);
            }
            System.out.println("finalString = " + finalString);
        }

        try(BufferedWriter bw = new BufferedWriter(fw)) {

            bw.write(finalString);
            bw.newLine();
            bw.write("--- END --- ");

            // not needed in try with resource statement
            bw.flush();
        }
    }
}