1

Why do I need to close Streams(FileInputStream etc..)? Does Java is not intelligent to use GC if I didn't close the stream?

public void getLongStrings() throws IOException {
        InputStream i1 = null;
        InputStream i2 = null;
        InputStreamReader isr1 = null;
        InputStreamReader isr2 = null;
        try {
            i1 = aBook.getInputStream();
            i2 = aNovel.getInputStream();
            isr1 = new InputStreamReader(i1);
            isr2 = new InputStreamReader(i2);
            foo = FileCopyUtils.copyToString(isr1);
            bar = FileCopyUtils.copyToString(isr2);
        }
        catch (IOException ioe) {
            //do something appropriate here
        } finally {
            if (i1 != null) i1.close();
            if (i2 != null) i2.close();
            if (isr1 != null) isr1.close();
            if (isr2 != null) isr2.close();
        }
    }

do I need to close all the streams I used?

  • yes you need to close stream because, the stream is already full with content and when you close the stream then you can use it again. also data is flush in drive when use flush method. when you close the stream JVM will see that stream is not can be use for further operation. – Musaddique S Feb 05 '16 at 09:29
  • 4
    Possible duplicate of [Closing Streams in Java](http://stackoverflow.com/questions/515975/closing-streams-in-java) – hpopiolkiewicz Feb 05 '16 at 09:30
  • 1
    When I leave your house should I close the door or should I assume you'll come behind me and handle it? – ChiefTwoPencils Feb 05 '16 at 09:33
  • 1
    You should look into [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) if it is the amount of code that you are railing against. – Andy Turner Feb 05 '16 at 09:38
  • 3
    Garbage collection is triggered when the JVM out of memory, not when it runs out of file descriptors. So relying on garbage collection to clean up for you may not leave you with enough descriptors when you need them. – Erwin Bolwidt Feb 05 '16 at 09:38

2 Answers2

3

Garbage collector is meant to collect unused objects. A stream is often linked to a lot of resources (file descriptor, socket, etc...) that are much more critical in your machine. Of course they are likely to be freed on program exit, but they should stay open for as little as possible

alex
  • 115
  • 6
0

This is called explicit termination, it manually finalizes an object that allocates resources, instead of using the finalizer method. I'm quoting here from Joshua Bloch's Effective Java:

So what should you do instead of writing a finalizer for a class whose objects encapsulate resources that require termination, such as files or threads? Just provide an explicit termination method, and require clients of the class to invoke this method on each instance when it is no longer needed. One detail worth mentioning is that the instance must keep track of whether it has been terminated: the explicit termination method must record in a private field that the object is no longer valid, and other methods must check this field and throw an Illegal- StateException if they are called after the object has been terminated. Typical examples of explicit termination methods are the close methods on InputStream, OutputStream, and java.sql.Connection. Another example is the cancel method on java.util.Timer,

Youssef Lahoud
  • 529
  • 3
  • 8