BufferedWriter and BufferedReader in Java
"
Introduction to BufferedReader and BufferedWriter Classes in Java
BufferedReader Class
The BufferedReader class in Java is used to read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. It is often used with a FileReader to read text from a file.
Key Points
- Read characters: Reads a single character.
- Read characters into a character array: Reads characters into a character array.
- Read a line of text: Reads an entire line of text.
Example
import java.io.*;
public class BufferedReaderExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedReader Class Methods
Method Name | Description |
---|---|
read() | Reads a single character. |
read(char[] cbuf) | Reads characters into a character array. |
read(char[] cbuf, int off, int len) | Reads characters into a portion of a character array. |
readLine() | Reads an entire line of text. |
skip(long n) | Skips n characters. |
mark(int readlimit) | Marks the present position in the stream. |
reset() | Repositions the stream to the last marked position. |
close() | Closes the stream. |
BufferedWriter Class
The BufferedWriter class in Java is used to write text to a character-output stream, buffering characters so as to provide for the efficient writing of characters, arrays, and strings. It is often used with a FileWriter to write text to a file.
Key Points
- Write a single character: Writes a single character.
- Write a character array: Writes a character array.
- Write a string: Writes a string.
- Flush the stream: Flushes the stream.
Example
import java.io.*;
public class BufferedWriterExample {
public static void main(String[] args) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"))) {
bw.write("Hello, world!");
bw.newLine();
bw.write("This is a new line.");
bw.flush(); // Ensure data is written to the file
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedWriter Class Methods
Method Name | Description |
---|---|
write(int c) | Writes a single character. |
write(char[] cbuf) | Writes a character array. |
write(char[] cbuf, int off, int len) | Writes a portion of a character array. |
write(String s) | Writes a string. |
newLine() | Writes a newline character. |
flush() | Flushes the stream. |
close() | Closes the stream. |
Why Use BufferedReader and BufferedWriter?
There are two main advantages to using BufferedReader and BufferedWriter:
- Improved Performance: By buffering characters, these classes reduce the number of system calls, leading to significant performance gains, especially when dealing with large files.
- Simplified File I/O: They provide convenient methods for reading and writing text, making file operations more straightforward.
Remember to close the streams after you're done using them to release system resources. You can use a try-with-resources block to ensure automatic closing.
Exercises
Write a program to write and read a character from a file using BufferedWriter and BufferedReader in Java
package bbsProject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class bbsdemo
{
public static void main(String[] args) throws IOException
{
File f = new File("output.txt");
BufferedWriter bwrite = new BufferedWriter(new FileWriter(f));
bwrite.write(115);
bwrite.close();
BufferedReader bread = new BufferedReader(new FileReader(f));
int data = bread.read();
System.out.println((char)data);
bread.close();
}
}
Output
s
Write a program to write and read a String from a file using BufferedWriter and BufferedReader in Java
package bbsProject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class bbsdemo
{
public static void main(String[] args) throws IOException
{
File f = new File("output.txt");
BufferedWriter bwrite = new BufferedWriter(new FileWriter(f));
bwrite.write("Satish");
bwrite.close();
BufferedReader bread = new BufferedReader(new FileReader(f));
String data = bread.readLine();
System.out.println(data);
bread.close();
}
}
Output
Satish
Write a program to write and read an array of Characters from a file using BufferedWriter and BufferedReader in Java
package bbsProject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
public class bbsdemo
{
public static void main(String[] args) throws IOException
{
File f = new File("output.txt");
BufferedWriter bwrite = new BufferedWriter(new FileWriter(f));
char c[] = {'a','b','c','d'};
bwrite.write(c);
bwrite.close();
BufferedReader bread = new BufferedReader(new FileReader(f));
char k[] = new char[4];
bread.read(k);
for(char v : k)
{
System.out.println(v);
}
bread.close();
}
}
Output
a
b
c
d
Write a Java Program to read multiples lines from a file using BufferedReader class
package bbsProject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class bbsdemo
{
public static void main(String[] args) throws IOException
{
File f = new File("output.txt");
BufferedReader bread = new BufferedReader(new FileReader(f));
String data;
while((data = bread.readLine())!=null)
{
System.out.println(data);
}
bread.close();
}
}
Output
Satish is teaching java
Satish also teaches web programming
Satish teaches cyber security