Java的Apache的POI java.lang.IllegalArgumentException异常:过去文件的结束位置21504 [英] Java apache POI java.lang.IllegalArgumentException: Position 21504 past the end of the file

查看:1818
本文介绍了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.

推荐答案

您正试图写入工作簿回从它被读取相同的文件路径名。看来,<一个href=\"http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/WorkbookFactory.html#create(java.io.File)\"相对=nofollow> WorkbookFactory.create 不释放资源,直到工作簿是关闭。

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.

请注意,为了正确地释放资源工作簿使用后应关闭。

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

在创建的FileOutputStream ,你已经有效地清除现有的文件,这样就可以将文件写入数据了。但是,工作簿仍然必须依靠原始文件是完好无损。然后,要写入的数据不再存在。

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()工作簿 ,释放资源。

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天全站免登陆