Character Stream in Java जावा में करैक्टर स्ट्रीम
Character Stream in Java is used to read and write character data (16-bit Unicode) instead of raw bytes.
जावा में करैक्टर स्ट्रीम का प्रयोग करैक्टर डेटा (16-बिट यूनिकोड) को पढ़ने और लिखने के लिए किया जाता है, न कि केवल बाइट्स के लिए।
Character Streams are mainly used for text files because they handle Unicode automatically.
करैक्टर स्ट्रीम्स का प्रयोग प्रायः टेक्स्ट फाइल्स के लिए किया जाता है क्योंकि ये यूनिकोड को स्वतः हैंडल करती हैं।
1️⃣ Important Character Stream Classes
Class Name | Description (English) | विवरण (Hindi) |
---|---|---|
FileReader |
Reads data from file | फाइल से डेटा पढ़ता है |
FileWriter |
Writes data to file | फाइल में डेटा लिखता है |
BufferedReader |
Reads data using buffer | बफर के साथ करैक्टर पढ़ता है |
BufferedWriter |
Writes data using buffer | बफर के साथ करैक्टर लिखता है |
PrintWriter |
Writes formatted text output | प्रिंट और प्रिंटलन मेथड्स के साथ लिखता है |
InputStreamReader |
Converts bytes to characters | बाइट को करैक्टर में बदलता है |
OutputStreamWriter |
Converts characters to bytes | करैक्टर को बाइट में बदलता है |
Reader |
Abstract class for character input | करैक्टर इनपुट स्ट्रीम्स का सुपरक्लास |
Writer |
Abstract class for character output | करैक्टर आउटपुट स्ट्रीम्स का सुपरक्लास |
2️⃣ Character Stream Examples
📝 Example 1: Read File using BufferedReader
import java.io.*;
class Sample {
public static void main(String args[]) {
try {
FileReader fr = new FileReader("file.txt");
BufferedReader br = new BufferedReader(fr);
int i;
while((i = br.read()) != -1) {
System.out.print((char)i);
}
br.close();
fr.close();
} catch(Exception e) {
System.out.println(e);
}
}
}
Output:
Hello World!
📝 Example 2: Write File using BufferedWriter
import java.io.*;
class Sample {
public static void main(String args[]) {
try {
FileWriter fw = new FileWriter("file.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Hello World!");
bw.close();
fw.close();
System.out.println("Data written successfully.");
} catch(Exception e) {
System.out.println(e);
}
}
}
Output:
Data written successfully.
file.txt → Hello World!
📝 Example 3: Write File using PrintWriter
import java.io.*;
class Sample {
public static void main(String args[]) {
try {
FileWriter fw = new FileWriter("file.txt");
PrintWriter pw = new PrintWriter(fw);
pw.write("Hello Friends!");
pw.close();
System.out.println("Data written successfully.");
} catch(Exception e) {
System.out.println(e);
}
}
}
Output:
Data written successfully.
file.txt → Hello Friends!
📝 Example 4: Simple File Write using FileWriter
import java.io.*;
class Sample {
public static void main(String args[]) {
try {
FileWriter fw = new FileWriter("file.txt");
fw.write("Hello World!");
fw.close();
System.out.println("Data written successfully.");
} catch(Exception e) {
System.out.println(e);
}
}
}
📝 Example 5: Simple File Read using FileReader
import java.io.*;
class Sample {
public static void main(String args[]) {
try {
FileReader fr = new FileReader("file.txt");
int i;
while((i = fr.read()) != -1) {
System.out.print((char)i);
}
fr.close();
} catch(Exception e) {
System.out.println(e);
}
}
}
Output:
Hello World!
Comments
Post a Comment