Java - File I/O in Hindi




दोस्तों इस chapter में हम Java File I/O के बारे में जानेंगे तो चलिए शुरू करते है. Java बाइट stream और फ़ाइल system के माध्यम से इनपुट और आउटपुट का समर्थन करने के लिए कई आसान I/O classes के साथ आता है. Java में हम files में डेटा को पढ़ सकते हैं और फ़ाइलों में डेटा को लिख भी सकते हैं, Java में कई इनपुट और आउटपुट stream हैं जो डेटा पढ़ने और लिखने के लिए उपयोग किए जाते हैं।

Java प्रोग्रामिंग language हमें कुछ ऐसी classes भी provide करती है जिनकी सहायता से हम input को किसी भी file से read कर सकते है और output को किसी भी file में store करा सकते है. दोस्तों मेरा कहने का यह मतलब है की यदि आप चाहे तो input console ना लेकर आप किसी file से read कर सकते है. और output console पर ना देख कर किसी file में store करके देख सकते है, फाइल्स के द्वारा इनपुट और आउटपुट ऑपरेशन्स perform करने के लिए आप stream का उपयोग करते है, data की sequence को stream कहा जाता है, Inputstream से input लिया जाता है, और output stream से output लिया जाता है।

जब data आइटम किसी computer सिस्टम में archived होते हैं, तो उन्हें समय-समय पर temporary या स्थायी रूप से अलग-अलग समय के लिए stored किया जा सकता है, फ़ाइलों USB ड्राइव, हार्ड डिस्क, ज़िप डिस्क, रील या compact डिस्क्स जैसे स्थायी storage डिवाइस पर मौजूद होती हैं, कंप्यूटर फाइलें कागज के इलेक्ट्रॉनिक equivalent हैं जो आमतौर पर कार्यालयों में फ़ाइल cabinet में संग्रहीत होती हैं।

Java file I/O को दो streams में विभाजित किया गया है -

  • Byte Streams

  • Character Stream

Byte Streams

यह byte के इनपुट और आउटपुट को संभालने के लिए सुविधाजनक साधन प्रदान करता है byte streams को 8-bit bytes से इनपुट और आउटपुट करने के लिए यूज़ किया जाता है।

Example

import java.io.*;
public class CopyFile {

   public static void main(String args[]) throws IOException {  
      FileInputStream in = null;
      FileOutputStream out = null;

      try {
         in = new FileInputStream("input.txt");
         out = new FileOutputStream("output.txt");
         
         int c;
         while ((c = in.read()) != -1) {
            out.write(c);
         }
      }finally {
         if (in != null) {
            in.close();
         }
         if (out != null) {
            out.close();
         }
      }
   }
}

Character Stream

Character stream का उपयोग करैक्टर data को रीड और write करने के लिए किया जाता है. यह input और characters के output को संभालने के लिए यह सुविधाजनक साधन प्रदान करता है character stream यूनिकोड का उपयोग करता है और इसलिए internationalization किया जा सकता है।

Example

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CopyCharacters {
    public static void main(String[] args) throws IOException {

        FileReader inputStream = null;
        FileWriter outputStream = null;

        try {
            inputStream = new FileReader("xanadu.txt");
            outputStream = new FileWriter("characteroutput.txt");

            int c;
            while ((c = inputStream.read()) != -1) {
                outputStream.write(c);
            }
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }
}