Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have to generate several excel files, but when using a
for
loop to create the different files, it throws an
EmptyFileException:the supplied file was empty
. The first time it generates an excel file, but later, it throws that exception. I have tried closing the
FileInputStream
, but it did not work.
public static void mergeExcelFiles(ArrayList<Employe> employeList, Enterprise enterprise, List<FileInputStream> list) throws IOException {
ArrayList<String> cell_formulas = generateCellToFormula();
File file = null;
for (int j = 0; j < employeList.size(); j++) {
XSSFWorkbook book = new XSSFWorkbook();
XSSFSheet sheet = null;
file = new File(listaEmpleados.get(j).getNombre() + ".xlsx");
try {
for (FileInputStream fin : list) {
XSSFWorkbook b = new XSSFWorkbook(fin);
for (int i = 0; i < b.getNumberOfSheets(); i++) {
sheet = book.createSheet(b.getSheetName(i));
copySheets(book, sheet, b.getSheetAt(i));
setDataWorkerInHoraryModel(book, enterpise, employeList.get(j));
passWeekendToSheets(book);
writeFile(book, file);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
–
–
https://poi.apache.org/apidocs/4.0/org/apache/poi/EmptyFileException.html
Exception thrown if an Empty (zero byte) file is supplied
for (FileInputStream fin : list) {
XSSFWorkbook b = new XSSFWorkbook(fin);
for (int i = 0; i < b.getNumberOfSheets(); i++) {
sheet = book.createSheet(b.getSheetName(i));
copySheets(book, sheet, b.getSheetAt(i));
The list you are passing has file that is 0 bytes.
========= Edited ===========
You are doing FileInputStream fin : list, there will be a lock on that file.
You need to close the inputstream if you want your system resources released back.
fin.close() is what you need, before the next iteration of the forloop.
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.