4

here is the code.

public static void main(String[] args) throws IOException {

    FileInputStream fis = null;

    fis = new FileInputStream(new File("D:\\za180s.ser"));
    // do something
    fis = new FileInputStream(new File("D:\\za185s.ser"));
    // do something
    fis = new FileInputStream(new File("D:\\za186s.ser"));
    // do something
    fis = new FileInputStream(new File("D:\\za187s.ser"));
    // do something
    fis.close();

}

the problem is : need i call fis.close() method after every "do something" or i just call fis.close() once after all.

ignore whether the close() position in finally and the code need try catch or not.

thx all.

nori
  • 41
  • 1
  • 3
  • why would you use the same inputstream for all files? – Stultuske Mar 11 '19 at 09:12
  • 3
    Yes, they all need to be closed. You could use Java's try-with-resources (https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) to not have to manually close your inputstreams. – nbokmans Mar 11 '19 at 09:12
  • Yes, you need to close them. You can use automatic resource management i.e. `try with resources` feature of Java 7 – AbhiN Mar 11 '19 at 09:15
  • The answers below (correctly) say that you do need to close it, but they haven't explained *why*. In general, not closing input streams can lead to [resource exhaustion](https://en.wikipedia.org/wiki/Resource_exhaustion_attack). – Andy Turner Mar 11 '19 at 09:17
  • because it is some fragment code from old project of company, it offen crash because “out of memory”. it needs refactoring optimization – nori Mar 11 '19 at 09:17

3 Answers3

10

Yes, you need to call close on each individual InputStream. The problem with your code is that you're reassigning the variable fis each time you create a new stream. In other words: fis no longer points to the old InputStream, so calling close will not close the previous stream.

For more information, check https://stackoverflow.com/a/40523/8819761

What you could also do is use Java 7's try-with-resources syntax, which will auto-close the stream once you exit the try block:

try (InputStream fis = new FileInputSteam(yourFile)) {
  // Do something
}

try (InputStream fis = new FileInputSteam(yourFile)) {
  // Do something else
}
Jeroen Steenbeeke
  • 3,884
  • 5
  • 17
  • 26
  • thanks ! old project of company, base on java6. leader won't let me to update the jdk on production environment. – nori Mar 11 '19 at 09:20
0

You have to do close everytime you finish working with InputStream.

In java, if you assign

 fis = new FileInputStream(new File("D:\\za180s.ser"));

fis will point to the new object so when you call fis.close() the old streams are not affected. And there is no way to close it.

Daniel Tran
  • 6,083
  • 12
  • 25
0

You need to call close method every time but don't worry now.From Java SE 7 you can use try-with-resources. As per Java-Oracle Doc,

The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

Please have a look on example.

static String readFirstLineFromFile(String path) throws IOException {
    try (BufferedReader br =
                   new BufferedReader(new FileReader(path))) {
        return br.readLine();
    }
}

In this example, the resource declared in the try-with-resources statement is a BufferedReader. The declaration statement appears within parentheses immediately after the try keyword. The class BufferedReader, in Java SE 7 and later, implements the interface java.lang.AutoCloseable. Because the BufferedReader instance is declared in a try-with-resource statement, it will be closed regardless of whether the try statement completes normally or abruptly (as a result of the method BufferedReader.readLine throwing an IOException).

For more details, Please have a look on Oracle-Java doc for try-with-resources.

https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

Muzzamil
  • 2,823
  • 2
  • 11
  • 23