Java Input & Output Streams जावा इनपुट एवं आउटपुट स्ट्रीम
In every programming language, there is a mechanism to handle input and output. In Java, input and output operations are mainly performed using streams, which are part of the java.io
package.
java.io
पैकेज का हिस्सा हैं।Basic Console I/O Streams in Java जावा में बेसिक कंसोल I/O स्ट्रीम्स
Java provides three standard I/O streams for console input and output:
जावा तीन मानक I/O स्ट्रीम्स प्रदान करता है कंसोल इनपुट और आउटपुट के लिए:
1️⃣ System.out
– Standard Output Stream मानक आउटपुट स्ट्रीम
-
This is used to print normal output to the console.
-
इसका उपयोग कंसोल पर सामान्य आउटपुट प्रिंट करने के लिए किया जाता है।
Example / उदाहरण:
public class Sample {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Output / आउटपुट:
Hello World
2️⃣ System.err
– Standard Error Stream मानक एरर स्ट्रीम
-
This is used to print error messages to the console.
-
इसका उपयोग कंसोल पर एरर संदेश प्रदर्शित करने के लिए किया जाता है।
Example / उदाहरण:
public class Sample {
public static void main(String[] args) {
System.err.println("Error occurred!");
}
}
Output / आउटपुट:
Error occurred!
⚡ ध्यान दें: कुछ IDEs और टर्मिनल्स पर एरर संदेश लाल रंग में दिखाई देते हैं।
3️⃣ System.in
– Standard Input Stream मानक इनपुट स्ट्रीम
-
This is used to take input from the keyboard.
-
इसका उपयोग कीबोर्ड से इनपुट लेने के लिए किया जाता है।
Example / उदाहरण:
import java.io.IOException;
public class Sample {
public static void main(String[] args) {
int i;
System.out.println("Enter any value:");
try {
i = System.in.read(); // Reads one byte from keyboard
System.out.println("You entered:");
System.out.println((char) i);
} catch (IOException e) {
System.out.println("Reading error");
}
}
}
Sample Output / नमूना आउटपुट:
Enter any value: 5
You entered:
5
💡 Key Points (मुख्य बातें)
-
System.in.read()
reads one byte at a time.
System.in.read()
एक समय में एक बाइट पढ़ता है। -
For reading full strings or numbers, use
Scanner
orBufferedReader
.
पूरे स्ट्रिंग या नंबर पढ़ने के लिएScanner
याBufferedReader
का प्रयोग करें। -
System.out
औरSystem.err
लगभग समान हैं, फर्क सिर्फ यह है किSystem.err
एरर को आउटपुट करता है।
Comments
Post a Comment