gpt4 book ai didi

异常 : (The process can not access the file because this file is used by another process)

转载 作者:行者123 更新时间:2023-11-29 08:33:22 25 4
gpt4 key购买 nike

在 JavaFx 中。我正在尝试使用 Apache POI 将 TableView 内容导出到 excel好吧,当我第一次点击按钮Export时,一切正常,tableView 的内容被导出,当我想打开导出的文件.xls 使用 excel 并尝试再次单击程序调用此异常:

    Caused by: java.io.FileNotFoundException: example.xls (
(The process can not access the file because this file is used by another process))
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
at hmproject.MenuController.Print(MenuController.java:7985)
... 66 more

.

这是我的代码:

  public void Print() throws JRException ,IOException,FileNotFoundException{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet spreadsheet = workbook.createSheet("sample");

HSSFRow row = null;

for (int i = 0; i < TousEmpView.getItems().size(); i++) {
row = spreadsheet.createRow(i);
for (int j = 0; j < TousEmpView.getColumns().size(); j++) {
row.createCell(j).setCellValue(TousEmpView.getColumns().get(j).getCellData(i).toString());
}
}
File file=new File("example.xls");
if(file.canRead())
{
FileOutputStream out = new FileOutputStream(file);//line of error

workbook.write(out);
out.close();
}else{

}

}

我理解这个异常,但我的问题是:

如何确认文件是否被其他进程使用?

我如何在 FileOutputStream 上进行这个测试?

最佳答案

你真的不能,它通常是基于操作系统的。

您可以查看此链接以获取更多信息 java-check-if-file-is-already-open

为避免此问题,您可以在文件名中添加增量。与时代有关的东西。系统.currentTimeMillis()。所以你的文件永远不会有相同的名字。您当然必须不时删除生成的文件

关于异常 : (The process can not access the file because this file is used by another process),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46010060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com