Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to close FileOutputStream which is wrapped by PrintStream?

Tags:

I'm using FileOutputStream with PrintStream like this:

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");         }     } } 

I'm closing only the PrintStream. Do I need to also close the FileOutputStream (out.close();)?

like image 663
hs2d Avatar asked Nov 10 '11 14:11

hs2d


People also ask

Does FileOutputStream need to be closed?

Yes, you do. While the garbage collector does close your FileOutputStream (by calling finalize ), it is not a good idea to rely on it because it runs unpredictably.

Why is it important to close the Fileinputstream and FileOutputStream after we are done processing the file?

OutputStream , say FileOutputStream , after writing to a file, if we don't close() the output stream, the data that we intended to write in the file remains in the buffer and is not written to the file. So it becomes necessary to close() an OutputStream .

Do we need to close OutputStream in Java?

Close an OutputStreamOnce you are done writing data to a Java OutputStream you should close it. You close an OutputStream by calling its close() method.

How do I close PrintStream?

The close() method of PrintStream Class in Java is used to close the stream. Closing a stream deallocates any value in it or any resources associated with it. The PrintStream instance once closed won't work. Also a PrintStream instance once closed cannot be closed again.


2 Answers

No, you only need to close the outermost stream. It will delegate all the way to the wrapped streams.

However, your code contains one conceptual failure, the close should happen in finally, otherwise it's never closed when the code throws an exception between opening and closing.

E.g.

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();     } } 

(note that I changed the code to throw the exception so that you understand the reason of the problem, the exception namely contains detailed information about the cause of the problem)

Or, when you're already on Java 7, then you can also make use of ARM (Automatic Resource Management; also known as try-with-resources) so that you don't need to close anything yourself:

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;     } } 
like image 111
BalusC Avatar answered Oct 26 '22 04:10

BalusC


No , here is implementation of PrintStream's close() method:

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;         }     } 

You can see out.close(); which closes output stream.

like image 29
Sergey Gazaryan Avatar answered Oct 26 '22 04:10

Sergey Gazaryan