जावा बफ़रडूटपुटस्ट्रीम (उदाहरण के साथ)

इस ट्यूटोरियल में, हम उदाहरणों की मदद से जावा बफ़रडूटपुटस्ट्रीम और इसके तरीकों के बारे में जानेंगे।

BufferedOutputStreamके वर्ग java.ioपैकेज अन्य आउटपुट स्ट्रीम डेटा लिखने के लिए (बाइट्स में) और अधिक कुशलता से साथ प्रयोग किया जाता है।

यह OutputStreamअमूर्त वर्ग का विस्तार करता है ।

BufferedOutputStream का कार्य करना

BufferedOutputStreamएक आंतरिक का कहना है 8192 बाइट्स के बफर

लिखने के संचालन के दौरान, बाइट डिस्क के बजाय आंतरिक बफर को लिखा जाता है। एक बार जब बफर भर जाता है या धारा बंद हो जाती है, तो पूरे बफर को डिस्क पर लिखा जाता है।

इसलिए, डिस्क पर संचार की संख्या कम हो जाती है। यही कारण है कि बाइट्स का उपयोग तेजी से होता है BufferedOutputStream

एक बफ़रडॉटपुटस्ट्रीम बनाएँ

बनाने के लिए BufferedOutputStream, हमें java.io.BufferedOutputStreamपहले पैकेज आयात करना चाहिए । एक बार जब हम पैकेज को इम्पोर्ट करते हैं तो हम आउटपुट स्ट्रीम कैसे बना सकते हैं।

 // Creates a FileOutputStream FileOutputStream file = new FileOutputStream(String path); // Creates a BufferedOutputStream BufferedOutputStream buffer = new BufferOutputStream(file); 

उपरोक्त उदाहरण में, हमने नामित फ़ाइल के BufferdOutputStreamसाथ एक नामित बफर बनाया है FileOutputStream

यहां, आंतरिक बफर में 8192 बाइट्स का डिफ़ॉल्ट आकार है। हालाँकि, हम आंतरिक बफर के आकार को भी निर्दिष्ट कर सकते हैं।

 // Creates a BufferedOutputStream with specified size internal buffer BufferedOutputStream buffer = new BufferOutputStream(file, int size); 

बफ़र फ़ाइलों को अधिक तेज़ी से बाइट्स लिखने में मदद करेगा।

BufferedOutputStream की विधियाँ

BufferedOutputStreamकक्षा में विभिन्न तरीकों के लिए कार्यान्वयन प्रदान करता है OutputStreamवर्ग।

लिखना () विधि

  • write() - आउटपुट स्ट्रीम के आंतरिक बफर के लिए एक एकल बाइट लिखता है
  • write(byte() array) - निर्दिष्ट सरणी से आउटपुट स्ट्रीम तक बाइट्स लिखते हैं
  • write(byte() arr, int start, int length) - स्थिति शुरू से शुरू होने वाले सरणी से आउटपुट स्ट्रीम की लंबाई के बराबर बाइट्स की संख्या लिखता है

उदाहरण: BufferedOutputStream किसी फ़ाइल में डेटा लिखने के लिए

 import java.io.FileOutputStream; import java.io.BufferedOutputStream; public class Main ( public static void main(String() args) ( String data = "This is a line of text inside the file"; try ( // Creates a FileOutputStream FileOutputStream file = new FileOutputStream("output.txt"); // Creates a BufferedOutputStream BufferedOutputStream output = new BufferedOutputStream(file); byte() array = data.getBytes(); // Writes data to the output stream output.write(array); output.close(); ) catch (Exception e) ( e.getStackTrace(); ) ) ) 

उपरोक्त उदाहरण में, हमने एक बफर आउटपुट स्ट्रीम बनाया है जिसका नाम आउटपुट है FileOutputStream। आउटपुट स्ट्रीम फ़ाइल output.txt के साथ जुड़ा हुआ है ।

 FileOutputStream file = new FileOutputStream("output.txt"); BufferedOutputStream output = new BufferedOutputStream(file); 

फ़ाइल में डेटा लिखने के लिए, हमने write()विधि का उपयोग किया है।

यहां जब हम प्रोग्राम चलाते हैं, तो output.txt फाइल निम्नलिखित सामग्री से भर जाती है।

 This is a line of text inside the file. 

नोट : getBytes()कार्यक्रम में प्रयुक्त विधि एक स्ट्रिंग को बाइट्स के एक सरणी में परिवर्तित करती है।

फ्लश () विधि

आंतरिक बफर को साफ करने के लिए, हम flush()विधि का उपयोग कर सकते हैं । यह विधि आउटपुट स्ट्रीम को बफर फाइल में मौजूद सभी डेटा को डेस्टिनेशन फाइल पर लिखने के लिए मजबूर करती है। उदाहरण के लिए,

 import java.io.FileOutputStream; import java.io.BufferedOutputStream; public class Main ( public static void main(String() args) ( String data = "This is a demo of the flush method"; try ( // Creates a FileOutputStream FileOutputStream file = new FileOutputStream(" flush.txt"); // Creates a BufferedOutputStream BufferedOutputStream buffer = new BufferedOutputStream(file); // Writes data to the output stream buffer.write(data.getBytes()); // Flushes data to the destination buffer.flush(); System.out.println("Data is flushed to the file."); buffer.close(); ) catch(Exception e) ( e.getStackTrace(); ) ) ) 

आउटपुट

 डेटा फ़ाइल में फ़्लश किया जाता है। 

जब हम प्रोग्राम चलाते हैं, फ़ाइल flush.txt स्ट्रिंग डेटा द्वारा प्रस्तुत पाठ से भर जाती है।

बंद () विधि

बफ़र किए गए आउटपुट स्ट्रीम को बंद करने के लिए, हम close()विधि का उपयोग कर सकते हैं । एक बार विधि कहा जाता है, हम डेटा लिखने के लिए आउटपुट स्ट्रीम का उपयोग नहीं कर सकते हैं।

अधिक जानने के लिए, Java BufferedOutputStream (आधिकारिक जावा प्रलेखन) पर जाएँ।

दिलचस्प लेख...