Java / Java File I/O and Serialization
Using BufferedReader and BufferedWriter
In this tutorial, you will learn how to use BufferedReader and BufferedWriter in Java. These classes provide an efficient way to read and write text data.
Section overview
5 resourcesExplores reading, writing, and manipulating files in Java.
1. Introduction
1.1 Goal of the Tutorial
In this tutorial, we aim to understand the use of BufferedReader and BufferedWriter classes in Java.
1.2 Learning Outcome
After this tutorial, you will be able to use BufferedReader for reading text from a character-input stream and BufferedWriter for writing text to a character-output stream.
1.3 Prerequisites
Basic understanding of Java programming and I/O Streams in Java is required.
2. Step-by-Step Guide
2.1 BufferedReader
BufferedReader is a class in Java that reads text from an input-stream (like a file) buffering characters so as to provide efficient reading of characters, arrays, and lines. The buffer size may be specified, but by default it's large enough for most purposes.
Example:
BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
2.2 BufferedWriter
BufferedWriter is a class in Java that writes text to an output-stream (like a file) buffering characters so as to provide efficient writing of single characters, arrays, and strings.
Example:
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
3. Code Examples
3.1 Example: Reading from a File
import java.io.*;
public class ReadFile {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
String line = null;
while((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
}
}
In this example, we read each line from a file named "input.txt" and print it on the console. The readLine() method returns null when the end of the file is reached.
3.2 Example: Writing to a File
import java.io.*;
public class WriteFile {
public static void main(String[] args) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
writer.write("This is a test line.");
writer.newLine();
writer.write("This is another test line.");
writer.close();
}
}
In this example, we write two lines into a file named "output.txt". The newLine() method is used to insert a newline.
4. Summary
In this tutorial, we have learned about reading from and writing to text files using BufferedReader and BufferedWriter in Java. We have seen how to iterate over each line in a file with BufferedReader and how to write lines to a file with BufferedWriter.
5. Practice Exercises
5.1 Exercise 1: Read a File and Count the Number of Lines
Write a Java program that reads a text file and prints the number of lines in the file.
5.2 Exercise 2: Copy Contents of One File to Another
Write a Java program that reads a text file and writes the contents into a new file.
5.3 Solutions
5.3.1 Solution to Exercise 1:
import java.io.*;
public class LineCount {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
int lines = 0;
while (reader.readLine() != null) lines++;
reader.close();
System.out.println("Number of lines: " + lines);
}
}
5.3.2 Solution to Exercise 2:
import java.io.*;
public class FileCopy {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
String line = null;
while((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
}
reader.close();
writer.close();
}
}
End of tutorial. Happy Learning!
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article