#
Working with Files in Java
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);
}
}
}
Info
- FileOutputStream & FileInputStream are working with all the types of files. Instead a text file we can have a picture for example.
- This type of streaming is not buffered. For buffering the operations we need to use BufferedInputStream and BufferedOutputStream classes.
#
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
Info
- BufferedReader : Creates a character-buffer for the input stream.
- BufferedWriter : Creates a character-buffer for the output stream.
- InputStreamReader & OutputStreamReader are used (under the hood) by the BufferedReader and BufferedWriter for the byte <-> char conversion.
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();
}
}
}
Info
ObjectInputStream and ObjectOutputStream are used to read/write Object from/into files. The object must implement Serializable interface and must have a no-args constructor.