File Reading & Writing in Java
In java, there multiple ways to read data from a file and to write data to a file. The most commonly used ways are as follows.
- Using Byte Stream (FileInputStream and FileOutputStream)
- Using Character Stream (FileReader and FileWriter)
Let's look each of these ways.
File Handling using Byte Stream
In java, we can use a byte stream to handle files. The byte stream has the following built-in classes to perform various operations on a file.
- FileInputStream - It is a built-in class in java that allows reading data from a file. This class has implemented based on the byte stream. The FileInputStream class provides a method read() to read data from a file byte by byte.
- FileOutputStream - It is a built-in class in java that allows writing data to a file. This class has implemented based on the byte stream. The FileOutputStream class provides a method write() to write data to a file byte by byte.
Let's look at the following example program that reads data from a file and writes the same to another file using FileInoutStream and FileOutputStream classes.
import java.io.*;
public class FileReadingTest {
public static void main(String args[]) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("C:\\Raja\\Input-File.txt");
out = new FileOutputStream("C:\\Raja\\Output-File.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
System.out.println("Reading and Writing has been success!!!");
}
catch(Exception e){
System.out.println(e);
}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
When we run the above program, it produce the following output.
File Handling using Character Stream
In java, we can use a character stream to handle files. The character stream has the following built-in classes to perform various operations on a file.
- FileReader - It is a built-in class in java that allows reading data from a file. This class has implemented based on the character stream. The FileReader class provides a method read() to read data from a file character by character.
- FileWriter - It is a built-in class in java that allows writing data to a file. This class has implemented based on the character stream. The FileWriter class provides a method write() to write data to a file character by character.
Let's look at the following example program that reads data from a file and writes the same to another file using FIleReader and FileWriter classes.
import java.io.*;
public class FileIO {
public static void main(String args[]) throws IOException {
FileReader in = null;
FileWriter out = null;
try {
in = new FileReader("C:\\Raja\\Input-File.txt");
out = new FileWriter("C:\\Raja\\Output-File.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
System.out.println("Reading and Writing in a file is done!!!");
}
catch(Exception e) {
System.out.println(e);
}
finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
When we run the above program, it produce the following output.