7
7
Concepts
Paths
BufferedWriter
BufferedReader
Introduction
In Java, we can read and write data from one file to another file. Working with files
is an important skill, as it allows us to read data from external sources and write data
to a persistent storage. This is useful for a variety of applications, including:
In this unit, we'll learn various classes and methods in Java to read and write data in
files.
1. Paths
In Java, the
Path interface from java.nio.file package represents a path to a file or
directory in the file system. It provides a way to manipulate files and directories in a
platform-independent manner, which means we can use the same code to work with
files and directories on different operating systems.
1. Relative path
2. Absolute path
1. Relative Path
A relative path is a path that specifies the location of a file or directory relative to the
current working directory.
2. Absolute Path
An absolute path is a path that specifies the exact location of a file or directory in the
file system, starting from the root directory.
The
Syntax
JAVA
1 file.getAbsolutePath();
Code
JAVA
1 import java.io.File;
2
3 class Main {
4 public static void main(String args
5 File file = new File("file.txt"
6 String absolutePath = file.getA
7
8 System.out.println(absolutePath
9 }
10 }
Sample Output
/home/rahul/documents/file.txt
file.txt and called the getAbsolutePath() method. Here, it points to the file
present in the same directory where the program is being executed.
2. BufferedWriter
The
BufferedWriter is a class from the java.io package that extends the abstract
class Writer . The BufferedWriter buffers characters to provide for the efficient
writing of single characters, arrays, and strings. The Writer writes text to a
character-output stream.
We can assume the buffer as a temporary storage area that is used to hold data before
it is written to the character-output stream (the actual data that is written to the file).
The
The constructor of
The
Syntax
JAVA
Here,
Code
JAVA
1 import java.io.BufferedWriter;
2 import java.io.FileWriter;
3 import java.io.IOException;
4
5 class Main {
6 public static void main(String[]
7 try {
8 BufferedWriter buffer =
9 } catch (IOException e) {
9 } catch (IOException e) {
10 System.out.println(e.getM
11 }
Expand
BufferedWriter object with a relative path of the file. It searches for the file
destination.txt in the current directory (in which the program is executed) and
creates a writer object. If the file is not found, it creates a new file.
write() method to write data to the buffer. When the buffer is full or when the
flush() or close() method is called, the data in the buffer is written to the
character-output stream and the buffer is emptied.
Code
JAVA
1 import java.io.BufferedWriter;
2 import java.io.FileWriter;
3 import java.io.IOException;
4
5 class Main {
6 public static void main(String[]
7 try {
8 BufferedWriter buffer =
9 buffer.write("Writing to
10 buffer.close(); // close
11 } catch (IOException e) {
Expand
In the above code, we have accessed the previously created file, written some text to
the buffer, and closed the bufferwriter with
close() method.
Once the
close() method is called, we cannot use the writer to write the data.
Code
JAVA
1 import java.io.BufferedWriter;
2 import java.io.FileWriter;
3 import java.io.IOException;
4 import java.util.Arrays;
5
6 class Main {
7 public static void main(String[]
8 int[] numbers = {1, 2, 3, 4,
9 try {
10 BufferedWriter bw = new
11 for (int number : number
Expand
1
2
3
4
5
destination.txt file is overwritten with the new data. This is because the
FileWriter as a default opens the existing file or creates a new one with
truncate (remove) mode. It removes any existing contents of the file.
If we want to append new data to the end of an existing file instead of overwriting it,
we can pass the
true value as the second argument to the FileWriter constructor. This will
open the file in append mode, which means that new data will be added to the end
of the file, rather than replacing the existing contents.
Syntax
JAVA
Other Methods
Method Description
3. BufferedReader
The
BufferedReader is a class from the java.io package extends the abstract class
Reader .
Similar to the
The
Syntax
JAVA
Here,
Code
JAVA
1 import java.io.BufferedReader;
2 import java.io.FileReader;
3 import java.io.FileNotFoundException
4
5 class Main {
6 public static void main(String[]
7 try {
8 BufferedReader buffer =
9 } catch (FileNotFoundExceptio
10 System.out.println(e.getM
}
11 }
Expand
BufferedReader object with a relative path of the file. It searches for the file
source.txt in the current directory (in which the program is executed) and creates
a writer object. If the file is not found, it throws an exception.
The
BufferedReader uses the Reader object to read data from a file and it stores
the data in an internal buffer.
The
1. read()
2. readLine()
Similar to
1. read()
The
read() method reads a single character from a file. It returns int data type.
Example 1: Reading a single character from the file and writing it to another file
Code
JAVA
1 import java.io.*;
2
3 class Main {
4 public static void main(String[]
5 try {
6 BufferedReader br = new
7 BufferedWriter bw new
7 BufferedWriter bw = new
8
9 int data = br.read();
10 bw.write(data);
11 br.close();
Expand
Hello World!
Code
JAVA
1 import java.io.*;
2
3 class Main {
4 public static void main(String[]
5 try {
6 BufferedReader br = new
7 char[] arr = new char[100
8
9 br.read(arr);
10 System.out.println(arr);
11 br.close();
Expand
Hello World!
Output
Hello World!
Note
2. readLine()
The
readLine() method is used to read a single line of text in the file. It returns
String data type.
Code
JAVA
1 import java.io.*;
2
3 class Main {
4 public static void main(String[]
5 try {
6 BufferedReader br = new
7 BufferedWriter bw = new
8
9 String line = br.readLine
10 bw.write(line);
11 br.close();
Expand
Hello World!
This is the second line.
Hello World!
readLine() method reads a line of text from the file and returns it as a string. We
then used the BufferedWriter class to write this line of text to the file
destination.txt . The write() method writes the specified string to the file.
Now that we have learned to read and write a specific length of content from the file.
Let's write a code that reads a whole file and writes it to another file.
Code
JAVA
1 import java.io.*;
2
3 class Main {
4 public static void main(String[]
5 try {
6 BufferedReader br = new
7 BufferedWriter bw = new
8
9 String line;
10 while ((line = br.readLi
11 bw.write(line);
Expand
Hello World!
This is the second line.
Hello World!
This is the second line.
readLine() method returns null , which indicates the end of the file. Finally, we
close the BufferedReader and BufferedWriter objects to release the resources.
Other Methods
Method Description
Summary
Paths
BufferedWriter
The FileWriter creates a new file if the specified file is not found.
BufferedReader