线程“主”中的异常java.io.IOException:流已关闭

时间:2019-03-06 06:09:09

标签: java excel

当我尝试从main方法执行以下代码时,我收到Stream Closed错误。但是我没有关闭代码中的任何流。我什至试图在for循环后关闭工作簿,但仍然得到相同的结果错误。谁能帮助我解决这个问题?

Code: 

public class ExcelCompare
{
    public void compare(String path1, String path2 ) throws IOException
    {
    FileInputStream fs = new FileInputStream(path1);
    XSSFWorkbook wk = new XSSFWorkbook(fs);
    XSSFSheet s = wk.getSheet("Sheet1");
    int rcount = s.getPhysicalNumberOfRows();
    FileInputStream fs1 = new FileInputStream(path2);
    XSSFWorkbook wk1 = new XSSFWorkbook(fs);
    XSSFSheet s1 = wk.getSheet("Sheet1");
    for(int i=1; i<rcount; i++)
    {
        XSSFRow r = s.getRow(i);
        XSSFRow r1 = s1.getRow(i);
        int ccount = r.getPhysicalNumberOfCells();
        for(int j=0;j<=ccount;j++)
        {
            XSSFCell c = r.getCell(j);
            XSSFCell c1 = r1.getCell(j);
            if(c.getCellType()==1)
            {
                if((c.getStringCellValue()).equals(c1.getStringCellValue()))
                    {

                    }
            }

            else
            {
                System.out.println("value mismatch"+ c.getStringCellValue() +c.getStringCellValue());
            }
        }
    }

    }

}

Error: 

Exception in thread "main" java.io.IOException: Stream Closed
    at java.io.FileInputStream.readBytes(Native Method)
    at java.io.FileInputStream.read(FileInputStream.java:255)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
    at org.apache.poi.util.BoundedInputStream.read(BoundedInputStream.java:121)
    at org.apache.poi.util.BoundedInputStream.read(BoundedInputStream.java:103)
    at org.apache.poi.util.IOUtils.copy(IOUtils.java:312)
    at org.apache.poi.util.IOUtils.peekFirstNBytes(IOUtils.java:70)
    at org.apache.poi.util.IOUtils.peekFirst8Bytes(IOUtils.java:57)
    at org.apache.poi.poifs.filesystem.FileMagic.valueOf(FileMagic.java:135)
    at org.apache.poi.openxml4j.opc.internal.ZipHelper.verifyZipHeader(ZipHelper.java:175)
    at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:209)
    at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:98)
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:324)
    at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:295)
    at excel.ExcelCompare.compare(ExcelCompare.java:21)
    at excel.ExcelExecute.main(ExcelExecute.java:13)

1 个答案:

答案 0 :(得分:0)

更新代码中的以下行即可使用

    FileInputStream fs1 = new FileInputStream(path2);
    XSSFWorkbook wk1 = new XSSFWorkbook(fs1);
    XSSFSheet s1 = wk1.getSheet("Sheet1");