diff --git a/src/main/java/com/example/springbatch/batch/ExcelRowWriter.java b/src/main/java/com/example/springbatch/batch/ExcelRowWriter.java index 8d5e363..5433940 100644 --- a/src/main/java/com/example/springbatch/batch/ExcelRowWriter.java +++ b/src/main/java/com/example/springbatch/batch/ExcelRowWriter.java @@ -19,11 +19,13 @@ public class ExcelRowWriter implements ItemStreamWriter { private Workbook workbook; private Sheet sheet; private int currentRowNumber; + private boolean isClosed; public ExcelRowWriter(String filePath) throws IOException { this.filePath = filePath; - currentRowNumber = 0; + this.isClosed = false; + this.currentRowNumber = 0; } @Override @@ -43,6 +45,10 @@ public class ExcelRowWriter implements ItemStreamWriter { @Override public void close() throws ItemStreamException { + if (isClosed) { + return; + } + try (FileOutputStream fileOut = new FileOutputStream(filePath)) { workbook.write(fileOut); } catch (IOException e) { @@ -52,6 +58,8 @@ public class ExcelRowWriter implements ItemStreamWriter { workbook.close(); } catch (IOException e) { throw new ItemStreamException(e); + } finally { + isClosed = true; } } }