Do I have to close FileInputStream?

天大地大妈咪最大 提交于 2019-12-22 01:14:28

问题


I am working as a trainee in Test Automation. I am working with creating Junit code with Eclipse and run using Eclipse. In that I am retriving the datas from excel sheet using FileInputStream function.

FileInputStream fi=new FileInputStream("c:\\search.xls");
Workbook w=Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);

Is it necessary to close the Inputstream function? If it so please guide me with some codings.


回答1:


Yes, you need to close the inputstream if you want your system resources released back.

FileInputStream.close() is what you need.




回答2:


FileInputStream fi=null;
try {
    fi=new FileInputStream("c:\\search.xls");
    Workbook w=Workbook.getWorkbook(fi);
    Sheet s=w.getSheet(0);
} finally {
    if (fi!=null) {
        fi.close();
    }
}



回答3:


You either need to close(), or end your program.

However you can run into confusing issues if you don't close the file as

  • sometimes test are run individually or a group of test are run in the same process. (So you could have a test which works one way but not the other)
  • you cannot rename or delete an open file.

It is best practice to always close your resources which you are finished with them, however I see unit tests as scripts which don't always have to follow best practice.




回答4:


It's always a good idea to close resources you use, BUT:

If you use resource A in resource B, it's sensible to close B instead of A if it has a method for it.

In your case, you use FileInputStream in Workbook, so you'd better to close Workbook and rely on Workbok that it will close FileInputStream.

In this particular case, actually, Workbook will close FileInputStream at the end of the getWorkbook() method but it's still a good idea to close Workbook to be able to be garbage collected.




回答5:


Yes! you should always release the resources once after you are done with them. Java has a powerful mechanism for Garbage Collection(note that it is different thing compare to resource management/leaks.) So a Garbage collector can not determine that if you need the resource in future or not? Failing to release resources may cause issues like- Denial of services, poor performance .

As already answered but another effort less way is try with resources

    try (FileInputStream fi = new FileInputStream("c:\\search.xls")) {

         //do something with fi.
         //fi.getChannel() ;

    } catch(IOException e) {
        // exception handling.
    } finally {
    // some statements for finally.
   }

Now you don't need to explicitly call fi.close() method.




回答6:


Recently, when I tried to refactor my code, I had to move the workbook creation to another method, and the FileInputStream is created in that method. That method creates a FileInputStream and returns a Workbook. But FileInputStream is not visible from the main method; so how will I close my FileInputStream at the end of main method? The answer is, you don't have to close FileInputStream, instead you just close the workbook, which internally closes FileInputStream. In short, it is incorrect to say that you must close FileInputStream no matter what.




回答7:


I do such a way to ensure to close the excel file input stream, this maybe helps

abstract int workWithWorkBook(Workbook workBook);

protected int doWorkBook(Path excelFile) throws IOException {
    File f = excelFile.toFile();

    try (FileInputStream excelContent = new FileInputStream(excelFile.toFile())){
        POIFSFileSystem fileSystem = new POIFSFileSystem(excelContent);
        Workbook workBook = null;
        if (f.getName().endsWith("xls")) {
            workBook = new HSSFWorkbook(fileSystem);
        } else if (f.getName().endsWith("xlsx")) {
            workBook = new XSSFWorkbook(excelContent);
        }
        return workWithWorkBook(workBook);

    }catch (Exception e){
        e.printStackTrace();
        throw e;
    }
}

9b9ea92b-5b63-47f9-a865-fd40dd602cd5




回答8:


Basic CompSci 101 tell us to make sure to close resources that we open, in Java or any language. So yes, you need to close them. Bad juju is bound to happen when you do not do so.

Also, you should learn (and have the inclination) to use the Javadocs. Look up at the Javadoc for FileInputStream and Closeable. The answers are there.



来源:https://stackoverflow.com/questions/5090937/do-i-have-to-close-fileinputstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!