XSSFWorkbook throwing EmtyFileException

1,369 views
Skip to first unread message

Sravanthi Kadam

unread,
Jan 8, 2019, 9:12:43 AM1/8/19
to Selenium Users
Hi all,

i am new bie to selenium. I am trying to write results data into xl file using apache poi assembly. Here is my code:

public class dummy {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub

String outFileath= "E:\\test.xls";

FileInputStream  fin;
FileOutputStream fos;

File outFile = new File(outFileath);
if(!outFile.exists())
{
outFile.createNewFile(); 
}

fin = new FileInputStream(outFile);  
fos = new FileOutputStream(outFile);

Workbook wb = new XSSFWorkbook(fin); // EmptyFileException  occuring here

fin.close();
FileOutputStream fsOut = new FileOutputStream(outFileath);
wb.write(fsOut);
wb.close();
fsOut.close();

}}
 I am getting the following excetion @ Workbook wb = new XSSFWorkbook(fin);

Exception in thread "main" org.apache.poi.EmptyFileException: The supplied file was empty (zero bytes long)
at org.apache.poi.util.IOUtils.peekFirstNBytes(IOUtils.java:82)
at org.apache.poi.util.IOUtils.peekFirst8Bytes(IOUtils.java:65)
at org.apache.poi.poifs.filesystem.FileMagic.valueOf(FileMagic.java:160)
at org.apache.poi.openxml4j.opc.internal.ZipHelper.verifyZipHeader(ZipHelper.java:143)
at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:175)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:104)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:299)
at org.apache.poi.ooxml.util.PackageHelper.open(PackageHelper.java:37)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:307)
at Webdriver.dummy.main(dummy.java:33)

please help me regarding this issue. I am unable to proceed further.

Thanks
Sravanthi

MASS NSEN

unread,
Jan 8, 2019, 9:20:00 AM1/8/19
to Selenium Users
Hi 

try this String outFileath= "E:/test.xls";

Sravanthi Kadam

unread,
Jan 9, 2019, 2:37:53 AM1/9/19
to Selenium Users
Hi

I tried as you suggested. Still I am getting the same exception. Do you have any other clues.

Thanks
Sravanthi

Tom Bhayya

unread,
Jan 10, 2019, 5:58:06 AM1/10/19
to Selenium Users
Hello Sravanthi,

I was able to check for the condition and create a new file if it does not exits using the following code:
You can put more login in the else condition to see if the file exit and then just write in the existing file.
When running you code it is creating an file but with empty data and I am not sure if that is reason it is creating a that exception. I am a newbie too :-)..
Trying to share what ever I could :-(
public static void main(String[] args) throws IOException {

        // TODO Auto-generated method stub
       
        //XSSFWorkbook wb = new XSSFWorkbook();
        String file = "C:\\TEST.xslx";
        String PrintData = "I am Testing";
        //FileInputStream fis = new FileInputStream("C:\\Users\\sgadwal\\Desktop\\TEST.xslx");
        File utFile = new File(file);
   
       
        if(!utFile.exists()){
            utFile.createNewFile();
            System.out.println("Created file");
            XSSFWorkbook wb = new XSSFWorkbook();
            XSSFSheet sheet = wb.createSheet("TestSheet");
            XSSFRow Row = sheet.createRow(0);
            XSSFCell Cell = Row.createCell(1);
            Cell.setCellValue(PrintData);
            FileOutputStream out = new FileOutputStream(file);
            //FileOutputStream out = new FileOutputStream(new File("TEST.xlsx"));
             wb.write(out);
             out.close();
             wb.close();
             System.out.println("END OF WRITING DATA IN EXCEL");
        }
}

Sravanthi Kadam

unread,
Jan 10, 2019, 6:11:15 AM1/10/19
to Selenium Users
Hi all,

Did anyone face this issue? If so, pl share how was it resolved. I am unable to proceed further.

Thanks
Sravanthi

Sravanthi Kadam

unread,
Jan 10, 2019, 6:13:20 AM1/10/19
to Selenium Users
Hi Tom

Thanks for the response. I will check on it.

Thank you.

Ankit Gupta

unread,
Jan 10, 2019, 7:28:59 AM1/10/19
to seleniu...@googlegroups.com
Please read this link, it would help:


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/c64d780c-8a58-45e0-a365-04d79e188973%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages