Java Files
FileReader
and FileWriter
are classes in the Java IO API (Application Programming Interface) that allow you to read and write files, respectively. They provide a convenient way to work with files as character streams.
FileReader
is used to read the contents of a file as a stream of characters. It is a subclass of the InputStreamReader
class, which is itself a subclass of Reader
. The FileReader
class takes a String
or a File
object as an argument to its constructor, specifying the name of the file to be read.
FileWriter
is used to write the contents of a file as a stream of characters. It is a subclass of OutputStreamWriter
class, which is itself a subclass of Writer
. The FileWriter
class takes a String
or a File
object as an argument to its constructor, specifying the name of the file to be written.
BufferedReader
and BufferedWriter
are classes that provide buffering for input and output streams, respectively. They can be used to improve the performance of reading and writing to a file.
BufferedReader
wraps an existing Reader
and buffers the input. It reads a large block of characters from the underlying stream and then allows the program to read them one at a time, which is much more efficient than reading one character at a time from the underlying stream. BufferedReader
has a readLine()
method that makes it easy to read the contents of a file one line at a time.
BufferedWriter
wraps an existing Writer
and buffers the output. It writes a large block of characters to the underlying stream, rather than writing one character at a time. This improves performance by reducing the number of calls to the underlying stream. BufferedWriter
has a newLine()
method that can be used to add a new line after each line of text that is written to a file.
Example program for reading and writing a file
import java.io.*;
public class FileReadWrite {
public static void main(String[] args) {
// The name of the file to open
String fileName = "examplefile.txt";
// This will reference one line at a time
String line = null;
try {
// FileReader reads text files in the default encoding
FileReader fileReader = new FileReader(fileName);
// Always wrap FileReader in BufferedReader
BufferedReader bufferedReader = new BufferedReader(fileReader);
// The name of the new file to create
String newFileName = "duplicatefile.txt";
// This will output the contents of the file to the new file
FileWriter fileWriter = new FileWriter(newFileName);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
while((line = bufferedReader.readLine()) != null) {
bufferedWriter.write(line);
bufferedWriter.newLine();
}
// Always close files
bufferedReader.close();
bufferedWriter.close();
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file '" + fileName + "'");
}
catch(IOException ex) {
System.out.println("Error reading file '" + fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
This program first defines the name of the file that it will read and the name of the new file that it will write. It then creates a FileReader
object to read the contents of the original file, and wraps it in a BufferedReader
object. This makes it easy to read the contents of the file one line at a time, using the readLine()
method of the BufferedReader
object.
The program then creates a FileWriter
object to write the contents of the file to the new file, and wraps it in a BufferedWriter
object. The write()
method is used to write the contents of the original file to the new file. The newLine()
method is used to add a new line after each line of text that is written to the new file.
The program uses a try-catch
block to handle any exceptions that may be thrown. If the file is not found, the program will print an error message to the console. If there is an error reading the file, the program will print another error message.
You can change the input file name and the outfile name, this is just a basic example to understand the flow, you can use different way to read and write a file as well, like FileInputStream and FileOutputStream, Path, etc.