Do I have to close FileInputStream?

后端 未结 8 726
广开言路
广开言路 2021-01-07 16:43

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 usi

相关标签:
8条回答
  • 2021-01-07 17:08
    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();
        }
    }
    
    0 讨论(0)
  • 2021-01-07 17:08

    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.

    0 讨论(0)
  • 2021-01-07 17:14

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

    FileInputStream.close() is what you need.

    0 讨论(0)
  • 2021-01-07 17:20

    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.

    0 讨论(0)
  • 2021-01-07 17:20

    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.

    0 讨论(0)
  • 2021-01-07 17:21

    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

    0 讨论(0)
提交回复
热议问题