Do I have to close FileInputStream?

后端 未结 8 809
广开言路
广开言路 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: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

提交回复
热议问题