Java apache POI java.lang.IllegalArgumentException:文件末尾的位置 21504 [英] Java apache POI java.lang.IllegalArgumentException: Position 21504 past the end of the file

查看:27
本文介绍了Java apache POI java.lang.IllegalArgumentException:文件末尾的位置 21504的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我做错了什么.我只是在读取和写入 Excel 文件,但总是出现此异常:

I don't know what am I doing wrong. I'm just reading and writing an Excel file, but always get this exception:

java.lang.IllegalArgumentException: 文件末尾的位置 21504

public class example {
  public static void main(String[] args) throws Exception {
    File destFile = new File("book.xls");
    Workbook destBook = WorkbookFactory.create(destFile);
    try {
        FileOutputStream out = new FileOutputStream(destFile);
        destBook.write(out);
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

book.xls 存在并且在从 A1 到 L50 的每个单元格中都有数字1".

book.xls exists and has the number "1" in each cell from A1 to L50.

推荐答案

您正试图将 Workbook 写回读取它的同一文件路径名.看来 WorkbookFactory.createWorkbook 关闭之前不会释放资源".

You are attempting to write the Workbook back to the same file pathname from which it was read. It appears that WorkbookFactory.create doesn't "release resources" until the Workbook is closed.

请注意,为了正确释放资源,Workbook 使用后应关闭.

Note that in order to properly release resources the Workbook should be closed after use.

当您创建FileOutputStream 时,您已经有效地擦除了现有文件,以便您可以将文件数据写出.但是,Workbook 仍然必须依赖于完整的原始文件.然后,要写入的数据不再存在.

When you create the FileOutputStream, you have effectively erased the existing file so that you can write the file data out. However, the Workbook must still rely on the original file being intact. Then, the data to write no longer exists.

您需要先写入不同的临时文件名.使用 Apache POI 3.11 或更高版本,因此您可以调用 close()Workbook 上,释放资源.

You will need to write to a different temporary filename first. Use Apache POI 3.11 or later, so you can call close() on the Workbook, releasing resources.

关闭从其读取工作簿的底层输入资源(文件或流).关闭后,不应再使用工作簿.

Close the underlying input resource (File or Stream), from which the Workbook was read. After closing, the Workbook should no longer be used.

这意味着在我们完成写入之前,原始文件必须存在,因此写入必须是另一个(临时)文件.

This means that the original file must exist until we're done writing it, so the write must be to another (temporary) file.

File srcFile = new File("book.xls");
File destFile = new File("booktemp.xls");
try {
   Workbook destBook = WorkbookFactory.create(srcFile);
   FileOutputStream out = new FileOutputStream(destFile);
   destBook.write(out);
   out.close();
   destbook.close();  // Available in Apache POI 3.11!
} catch (Exception e) {
   e.printStackTrace();
}

然后就可以删除原来的文件,把新创建的临时文件重命名为原来的名字.

Then you can delete the original file and rename the newly created temporary file to the original name.

boolean deleteSuccess = srcFile.delete();
boolean renameSuccess = destFile.renameTo(srcFile);

这篇关于Java apache POI java.lang.IllegalArgumentException:文件末尾的位置 21504的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆