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 (मुख्य बातें)

  1. System.in.read() reads one byte at a time.
    System.in.read() एक समय में एक बाइट पढ़ता है।

  2. For reading full strings or numbers, use Scanner or BufferedReader.
    पूरे स्ट्रिंग या नंबर पढ़ने के लिए Scanner या BufferedReader का प्रयोग करें।

  3. System.out और System.err लगभग समान हैं, फर्क सिर्फ यह है कि System.err एरर को आउटपुट करता है।

Comments

Popular posts from this blog

What is a Web Browser? वेब ब्राउज़र क्या है?

Java's Support System जावा का सहयोगी तंत्र

The Internet and Java इंटरनेट और जावा का सम्बन्ध