2011-11-10 15 views
14

मैं FileOutputStream उपयोग कर रहा हूँ PrintStream साथ इस तरह:क्या मुझे FileOutputStream को बंद करना है जो प्रिंटस्ट्रीम द्वारा लपेटा गया है?

class PrintStreamDemo { 
    public static void main(String args[]){ 
    FileOutputStream out; 
    PrintStream ps; // declare a print stream object 
    try { 
    // Create a new file output stream 
    out = new FileOutputStream("myfile.txt"); 

    // Connect print stream to the output stream 
    ps = new PrintStream(out); 

    ps.println ("This data is written to a file:"); 
    System.err.println ("Write successfully"); 
    ps.close(); 
    } 
    catch (Exception e){ 
    System.err.println ("Error in writing to file"); 
    } 
    } 
} 

इम समापन केवल PrintStream। क्या मुझे FileOutputStream (out.close();) को बंद करने की आवश्यकता है?

+0

स्रोत कोड देखें: http://www.docjar.com/html/api/java/io/PrintStream.java.html – jtoberon

+0

वैसे, प्रिंटस्ट्रीम की सुंदरता यह तथ्य है कि आप इसका उपयोग केवल इसके साथ कर सकते हैं एक स्ट्रिंग (फ़ाइल नाम के लिए) या एक फ़ाइल ऑब्जेक्ट। प्रिंटस्ट्रीम में इसका उपयोग करने के लिए आपको केवल FOStream खोलने की आवश्यकता नहीं है। – Mechkov

उत्तर

22

नहीं, आप केवल सबसे बाहरी धारा को बंद करने की जरूरत है। यह लपेटा धाराओं के लिए सभी तरह से प्रतिनिधि होगा।

हालांकि, आपके कोड में एक वैचारिक विफलता है, बंद finally में होना चाहिए, अन्यथा यह कभी भी बंद नहीं होता है जब कोड खोलने और बंद करने के बीच अपवाद फेंकता है।

उदा।

public static void main(String args[]) throws IOException { 
    PrintStream ps = null; 

    try { 
     ps = new PrintStream(new FileOutputStream("myfile.txt")); 
     ps.println("This data is written to a file:"); 
     System.out.println("Write successfully"); 
    } catch (IOException e) { 
     System.err.println("Error in writing to file"); 
     throw e; 
    } finally { 
     if (ps != null) ps.close(); 
    } 
} 

या (ध्यान दें कि मैं फेंक अपवाद ताकि आप समस्या का कारण समझने के लिए कोड बदल गया है, अपवाद अर्थात् समस्या के कारण के बारे में विस्तृत जानकारी होती है) (स्वचालित संसाधन प्रबंधन, यह भी try-with-resources के रूप में जाना जाता है), जब आप जावा 7 कगार पर हैं, तो आप भी एआरएम का उपयोग कर सकते हैं ताकि आप कुछ भी अपने आप को बंद करने के लिए की जरूरत नहीं है:

public static void main(String args[]) throws IOException { 
    try (PrintStream ps = new PrintStream(new FileOutputStream("myfile.txt"))) { 
     ps.println("This data is written to a file:"); 
     System.out.println("Write successfully"); 
    } catch (IOException e) { 
     System.err.println("Error in writing to file"); 
     throw e; 
    } 
} 
+0

जब मैं अंत में ब्लॉक और 'ps.close करने की कोशिश जोड़ें()' मैं वहाँ में त्रुटि मिलती है: 'चर ps – hs2d

+0

आप' null' के साथ प्रारंभ करने की आवश्यकता initialized' नहीं हो सकता। – BalusC

+0

आह, उस बारे में सोचा नहीं, धन्यवाद! – hs2d

3

नहीं, जावाडोक के अनुसार, करीब विधि अंतर्निहित धारा आप के लिए close होगा।

4

नहीं, आपको इसकी आवश्यकता नहीं है। PrintStream.close विधि स्वचालित रूप से अंडरलाइनिंग आउटपुट स्ट्रीम को बंद कर देती है।

चेक एपीआई।

public void close() { 
    synchronized (this) { 
     if (! closing) { 
     closing = true; 
     try { 
      textOut.close(); 
      out.close(); 
     } 
     catch (IOException x) { 
      trouble = true; 
     } 
     textOut = null; 
     charOut = null; 
     out = null; 
     } 
    } 

आप out.close(); जो उत्पादन धारा बंद कर देता है देख सकते हैं:

http://download.oracle.com/javase/6/docs/api/java/io/PrintStream.html#close%28%29

5

नहीं है, यहाँ PrintStream के close() विधि के क्रियान्वयन है।

संबंधित मुद्दे