1. Read Data Using BufferReader
This program demonstrates the use of BufferedReader to read text entered by the user. It reads a line of input and displays it on the console.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BufferedReaderExample {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.print("Enter Your Name: ");
String name = br.readLine();
System.out.println("Welcome " + name);
}
}Input and Output:
Enter Your Name: Rahul
Welcome Rahul
2. Write Data to a File Using BufferedWriter
This program demonstrates the use of BufferedWriter to write text into a file. The entered text is stored in the specified file.
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterExample {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(
new FileWriter("message.txt"));
bw.write("Welcome to Java Programming");
bw.newLine();
bw.write("BufferedWriter Example");
bw.close();
System.out.println("Data written successfully.");
}
}Output:
Data written successfully.
File(message.txt) Output:
Welcome to Java Programming
BufferedWriter ExampleCode language: CSS (css)
3. Read Characters Using InputStreamReader
This program demonstrates the use of InputStreamReader to read characters from the keyboard. It converts byte input into character data.
import java.io.IOException;
import java.io.InputStreamReader;
public class InputStreamReaderExample {
public static void main(String[] args) throws IOException {
InputStreamReader reader =
new InputStreamReader(System.in);
System.out.print("Enter a Character: ");
int ch = reader.read();
System.out.println("You Entered: " + (char) ch);
reader.close();
}
}Input and Output:
Enter a Character: A
You Entered: A
4. Write Characters Using OutputStreamWriter
This program demonstrates the use of OutputStreamWriter to write character data. It converts characters into bytes before displaying them on the console.
import java.io.IOException;
import java.io.OutputStreamWriter;
public class OutputStreamWriterExample {
public static void main(String[] args) throws IOException {
OutputStreamWriter writer =
new OutputStreamWriter(System.out);
writer.write("Welcome to Java OutputStreamWriter");
writer.flush();
writer.close();
}
}Output:
Welcome to Java OutputStreamWriter
5. Copy File Using BufferedReader and BufferedWriter
This program copies the contents of one text file into another. It reads each line using BufferedReader and writes it using BufferedWriter.
import java.io.*;
public class FileCopyBuffered {
public static void main(String[] args) throws IOException {
BufferedReader br =
new BufferedReader(new FileReader("source.txt"));
BufferedWriter bw =
new BufferedWriter(new FileWriter("destination.txt"));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
br.close();
bw.close();
System.out.println("File copied successfully.");
}
}source.txt
Welcome to Java Programming
destination.txt
Welcome to Java Programming
Output:
File copied successfully.Code language: CSS (css)
