Java apache POI java.lang.IllegalArgumentException:文件末尾后的位置 21504
Java apache POI java.lang.IllegalArgumentException: Position 21504 past the end of the file
我不知道我做错了什么。我只是在读写一个 Excel 文件,但总是得到这个异常:
java.lang.IllegalArgumentException: Position 21504 past the end of the file
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”。
您正在尝试将 Workbook
写回到读取它的同一文件路径名。似乎 WorkbookFactory.create
在 Workbook
关闭之前不会 "release resources"。
Note that in order to properly release resources the Workbook should be closed after use.
当您创建 FileOutputStream
时,您已经有效地删除了现有文件,以便您可以将文件数据写出。但是,Workbook
必须仍然依赖于原始文件的完好无损。那么,要写入的数据就不存在了。
您需要先写入不同的临时文件名。使用 Apache POI 3.11 或更高版本,因此您可以调用 close()
on the Workbook
,释放资源。
Close the underlying input resource (File or Stream), from which the Workbook was read. After closing, the Workbook should no longer be used.
这意味着原始文件必须存在,直到我们完成写入,因此写入必须是另一个(临时)文件。
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();
}
然后就可以删除原来的文件,把新建的临时文件重命名为原来的名字
boolean deleteSuccess = srcFile.delete();
boolean renameSuccess = destFile.renameTo(srcFile);
在 create 方法中传递 FileInputStream
对象而不是 File 对象。它会起作用。
public static void main(String[] args) throws Exception
FileInputStream destFile = new FileInputStream("book.xls");
Workbook destBook = WorkbookFactory.create(destFile);
try {
FileOutputStream out = new FileOutputStream(destFile);
destBook.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
我不知道我做错了什么。我只是在读写一个 Excel 文件,但总是得到这个异常:
java.lang.IllegalArgumentException: Position 21504 past the end of the file
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”。
您正在尝试将 Workbook
写回到读取它的同一文件路径名。似乎 WorkbookFactory.create
在 Workbook
关闭之前不会 "release resources"。
Note that in order to properly release resources the Workbook should be closed after use.
当您创建 FileOutputStream
时,您已经有效地删除了现有文件,以便您可以将文件数据写出。但是,Workbook
必须仍然依赖于原始文件的完好无损。那么,要写入的数据就不存在了。
您需要先写入不同的临时文件名。使用 Apache POI 3.11 或更高版本,因此您可以调用 close()
on the Workbook
,释放资源。
Close the underlying input resource (File or Stream), from which the Workbook was read. After closing, the Workbook should no longer be used.
这意味着原始文件必须存在,直到我们完成写入,因此写入必须是另一个(临时)文件。
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();
}
然后就可以删除原来的文件,把新建的临时文件重命名为原来的名字
boolean deleteSuccess = srcFile.delete();
boolean renameSuccess = destFile.renameTo(srcFile);
在 create 方法中传递 FileInputStream
对象而不是 File 对象。它会起作用。
public static void main(String[] args) throws Exception
FileInputStream destFile = new FileInputStream("book.xls");
Workbook destBook = WorkbookFactory.create(destFile);
try {
FileOutputStream out = new FileOutputStream(destFile);
destBook.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}