stream close « API « Java I/O Q&A

1. Closing Streams in Java

why do we need to close a FileInputStream (and streams in general) in any case before we leave the program? What would happen otherwise? If the program stops before the input ...

2. Best way to close nested streams in Java?

What is considered the best, most comprehensive way to close nested streams in Java? For example, consider the setup:

FileOutputStream fos = new FileOutputStream(...)
BufferedOS bos = new BufferedOS(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos);
I ...

3. Java: Making reference null after closing stream

Is it a good practice to set stream references to null after closing them? Would this release resources in any way? Example:

BufferedReader input= new BufferedReader(new FileReader("myfile.txt"));

// code

input = null;

// possible more code

4. Method may fail to close stream on exception

I get the critical error with finbugs The method creates an IO stream object, does not assign it to any fields, pass it to other methods, or return it, and does not ...

5. How close Java Input Streams?

In the following code:

DataInputStream in = new DataInputStream(
          new BufferedInputStream(new FileInputStream(file)));
do I need to close the 2 other stream in addition to ...

6. How do I refactor closing a stream in Java?

Due to my company's policy of using Eclipse and using Eclipse's code-autofix, the following code pattern appears excessively in the codebase:

InputStream is = null;
try {
    is = url.openConnection().getInputStream();

7. Stream closed exception in java

Please look at what I have done

private InputSource getContent(String fName) throws SAXException, IOException, ServiceException {
        // Some code here

8. FlyingSaucer renderer.setDocument throws "Stream closed" exception

I am having problems with creating a PDF using the simple example found here. It is my first time trying to use it and I have tried a few ...

9. closing the streams of I/o

what are the bad things happen when i don't close the stream does the close operation automatically flushes are all the streams closed after progarm exits thanks in advance

10. Do we always have to close the stream?

Have a FileInputStream, I am wondering whether I need to close it or not? If yes, if I pass this stream object to the other method, can I close in that ...

11. How to close std-streams from java.lang.Process appropriate?

This question is about java.lang.Process and its handling of stdin, stdout and stderr. We have a class in our project that is an extension to There we have a quiet new ...

12. Will close one of three linked streams close them all?

As far as I can tell, for all the stream classes which wrap around underlying streams, a call to close() for the outer (higher-level) stream will also close the inner (lower-level) stream. So you should be able to get away with simply calling z.close(); nothing else is needed. However, this behavior does not seem to be explicitly guaranteed by the API ...

13. Question on closing wrapped streams

I've read two opposing views on what's required when closing wrapped streams, and I'd like to get to the bottom of it. One view is that each stream has to be closed explicitly, in the opposite order that they were opened. The opposing view is that closing the outer-most stream is all that's needed; it calls close() on all wrapped streams ...

14. closing streams

Dear All, Could you tell me if i am closing the inputstream ,outputstream etc.. correctly or not. I want to ensure that all the open streams gets closed(and also flushed ) properly.I am closing out.close in try block and also in finally block also. Is it correct to do so OR should i close out,out1,in only in finally. Also what if ...

15. Closing application close all stream?

16. Closing Stream

17. close stream

18. How many close statements are required for nested streams?

Given code like this: File outFile = "some file name"; FileOutputStream fout = new FileOutputStream(outFile); BufferedOutputStream bout= new BufferedOutputStream(fout); OutputStreamWriter out = new OutputStreamWriter(bout); How many close statements are required? In most code I have looked at only the outermost stream is closed. Here it would be 'out'. But occasionally I have seen all three being explicitly closed. Thanks, -=beeky

19. Closing Chained Streams

If I do something like this: InputStream is = con.openInputStream(); BufferedInputStream bis = new BufferedInputStream(is); MyBufferedInputSream mybis = new BufferedInputStream(bis); etc., etc. Can I just close the last stream (mybis) or should I close each one? What if I just close the original InputStream (is)? Will all the other streams close and get cleaned up by the garbage collector? Thanks! Drew ...

20. when terminated the program with Control + c, how to close all open file streams?

Hi, I write a very simple program that create a dummy log file. I have a BufferedWriter object and writes dummy line to log file. I run this program on linux on command line and when i press the control + c, my open bufferedWriter stream is not closed. I close the BufferedWriter object on finally block, but if i press ...

21. How should streams be closed?

Hi. I am having this question because I noticed something while working on a Linux system. Consider this code: FileInputStream in = ...; // an exception is thrown // done reading in.close(); // this line is skipped because of the exception When I debugged, and the close() was skipped when an exception happened, lsof (list open files) command showed that ...

24. Findbugs complains of streams might not get closed. LineNumberReader

Hi Guys, whats wrong with this code ? public static String getFileContents(String filename) throws IOException { LineNumberReader lnReader = null; FileReader fReader = null; StringBuffer buffer = null; try { fReader = new FileReader(filename); lnReader = new LineNumberReader(fReader); //// FIND BUGS complains at this line. /** Bad practice - Method may fail to close stream on exception : OS: getFileContents(String) may ...

25. Stream Closed Problem

Hello! The problem is I want to close the stream where I read the message - in "getInput(int type)" method. But closing the stream as below - or even in the finally block - gives me a Stream Closed exception. I cannot understand why am I getting this error , after all I am creating a new Stream and a new ...

26. Does the order of stream closing matters?

ByteArrayOutputStream bo = new ByteArrayOutputStream(); ObjectOutputStream o = null; try { o = new ObjectOutputStream(bo); o.writeObject(ob); o.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if(bo != null) { try { bo.close(); } catch (IOException e) { e.printStackTrace(); } } try { if(o != null) { o.close(); } } catch (IOException e) { e.printStacktrace(); } }

27. stream closed before entering a String

28. How to close stream on closing window ?

29. Questions on Stream Closed

Hi cotton.m, Thank you so much for your reply. Yep ok so I also closed the underlying streams, so is the design of the code wrong? I mean how could I use the stream again? I just thought that by calling the new InputStreamReader( would again open that stream. Can you please further explain on what would by my code lacks ...

30. close stream

31. I/O error: InputSource: Stream closed

The parser closes the input source, having read it all the way to EOF. So there's nothing left to be read even if the parser didn't close the source. So you need a new input source. But are the changes taking place on the file, or just in the DOM? In which case re-reading the file won't show any changes - ...

32. Closed stream

I also believe you have a multithreading issue. You can't use the synchronized keyword for each method (method1 and method2) since you will still have a threading problem. I believe you need to synchronize on the file object which only one method at a time can get and use. If you search google for something like 'java synchronize file object', you ...

33. Closing a Stream

34. dose Process need close stream??

I'm not sure I understand what you are saying BUT if one does not process the stdout and stderr streams on can get a deadlock. You should read read the 4 sections of [|] and implement ALL the recommendations. P.S. It is sometimes (but rarely) necessary to explicitly close the Process stdin stream. Edited by: sabre150 on Aug 21, 2009 11:44 ...

35. Stream closed error

A couple of options: 1. Make the method actually open the file, so it'll take filename/File object/whatever instead of an InputStream. This is not optimal, since it requires you to work with Files instead of plain Streams. 2. Read the data once and buffer it somewhere, only doing the reading once. If you're dealing with large amounts of data, you might ...

36. Stream closing

I have been dealing with this all day, and have not found an answer even on these forums. In short, when calling getInputStream() on a JarEntry, it is returning null. It is not documented anywhere that I can find in the javadoc that indicates a situation where .getinputStream() ever returns null, but it most definitely and certainly is. Any tips?

37. close every stream or just one?

38. Exception in Cipheroutput stream's close

The exception certainly looks to be happening on the close() because that is the only place the doFinal() is likely to be called. There is nothing obviously wrong with the code and I use similar code all the time. Since we can't see what algorithms you are using (cipher, block mode, padding etc) your best bet is to produce a short ...

39. Closing a buffered character stream

40. Closing and Re-opening a stream

Hey, I know this could be classed as double posting, but it's a lot more specific than my original post, sorry anyway. I want to know how I can, using sockets, inform the client that the server has finished sending the current information. The EOF is not an option because the server then sends more information, such as "Thank-you for receiving ...

41. closing stream 'problem'

42. Closing an anonymous stream in method call

The command does not keep a reference to the stream hence there are no references to it and hence it will be GC'ed after the method returns as that is the extent of the streams scope. As the others have said I would highly reccomend handling it explicitly, create and close that stream yourself. Then, you can be sure what ...