chivas_hvn
chivas_hvn

Reputation: 371

Java - Apache POI - Read/Write .xlsx file - File getting corrupted and becomes empty

I am using a Java program to read and write an existing .xlsx file(same file) but the file is getting corrupted, and the file size becomes zero bytes which is causing "org.apache.poi.EmptyFileException: The supplied file was empty (zero bytes long)”.

One more thing is - this is not happening consistently. The program is reading and writing to file properly most of the time but occurring once out of 10-15 runs. It would be helpful if anyone has a solution to this. BTW, am using Apache POI 3.13.

Read File Program:

public String getExcelData(String sheetName, int rowNum, int colNum){
    String retVal = null;
    try {
        FileInputStream fis = new FileInputStream("/Absolute/File/Path/Test-File.xlsx");
        Workbook wb = WorkbookFactory.create(fis);
        Sheet s = wb.getSheet(sheetName);
        Row r = s.getRow(rowNum);
        Cell c = r.getCell(colNum);
        retVal=(c.getStringCellValue());
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return retVal;

Write File Program :

public void writeToExcel(String sheetName,int rowNum,int cellNum,String desc){
    try {
        FileInputStream fis = new FileInputStream("/Absolute/File/Path/Test-File.xlsx");
        Workbook wb = WorkbookFactory.create(fis);
        Sheet s = wb.getSheet(sheetName);
        Row r = s.getRow(rowNum);
        Cell c = r.createCell(cellNum);
        c.setCellValue(desc);
        FileOutputStream fos = new FileOutputStream("/Absolute/File/Path/Test-File.xlsx");
        wb.write(fos);
        fos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Error Trace:

Exception in thread "main" org.apache.poi.EmptyFileException: The supplied file was empty (zero bytes long)
at org.apache.poi.util.IOUtils.peekFirst8Bytes(IOUtils.java:55)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:201)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:168)
at ExcelLibrary.getExcelData(ExcelLibrary.java:139)
at Driver.main(Driver.java:82)

Upvotes: 7

Views: 34846

Answers (6)

Subhaneil Sengupta
Subhaneil Sengupta

Reputation: 3

Try this one out. I was facing the same issue but resolved it with below.

FileInputStream fis=new FileInputStream(-Location-);
--- do whatever you want to do --
fis.close();
FileOutputStream fos=new FileOutputStream(-Location-);
wb.write(fos);
fos.close();
wb.close();

Upvotes: 0

Dheeraj Upadhyay
Dheeraj Upadhyay

Reputation: 328

I faced this issue initially, solution is simple.

I am writting only code line which u need to add fis.close(); wb.close(); before returning retval

public String getExcelData(String sheetName, int rowNum, int colNum){
String retVal = null;
try {
    FileInputStream fis = new FileInputStream("/Absolute/File/Path/Test-File.xlsx");
    Workbook wb = WorkbookFactory.create(fis);
    Sheet s = wb.getSheet(sheetName);
    Row r = s.getRow(rowNum);
    Cell c = r.getCell(colNum);
    retVal=(c.getStringCellValue());
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (InvalidFormatException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
fis.close();
wb.close();
return retVal;    
}

Upvotes: 0

Ataur Rahman Munna
Ataur Rahman Munna

Reputation: 3915

As a java dev, you need to use finally block when you use try-catch block. in your finally block, you must close FileInputStream and FileOutputStream.may be filehandler opened as you didn't close.

Upvotes: 0

TouDick
TouDick

Reputation: 1322

You are reading and writing to the same file at the same time. Try to first close FileInputStream fis before writing to FileOutputStream fos. Or use temporary file to write new result and then rename it to original one.

BTW. close automatically performs flush, so it don't has to be called separately.

Upvotes: 4

PhstKv
PhstKv

Reputation: 37

you need to close the FileInputStream and FileOutputStream with org.apache.commons.io.IOUtils.closeQuietly(fos) and fis.

Upvotes: 3

Valentin Rocher
Valentin Rocher

Reputation: 11669

When you write to your file, you seem not to be using flush. Furthermore, your close code should be done in a finally block to ensure the stream is closed even if something wrong happens.

Upvotes: 0

Related Questions