Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaException:(进程无法访问该文件,因为该文件已被另一进程使用)_Java_Excel_Apache Poi_Javafx 2 - Fatal编程技术网

JavaException:(进程无法访问该文件,因为该文件已被另一进程使用)

JavaException:(进程无法访问该文件,因为该文件已被另一进程使用),java,excel,apache-poi,javafx-2,Java,Excel,Apache Poi,Javafx 2,在JavaFx中。我正在尝试使用将表格视图内容导出到excel。当我第一次单击按钮“导出”时,一切正常,表格视图的内容被导出,当我想使用excel打开导出的文件.xls并再次尝试单击时,程序调用此异常: Caused by: java.io.FileNotFoundException: example.xls ( (The process can not access the file because this file is used by another process))

在JavaFx中。我正在尝试使用将表格视图内容导出到excel。当我第一次单击按钮“导出”时,一切正常,表格视图的内容被导出,当我想使用excel打开导出的文件.xls并再次尝试单击时,程序调用此异常:

    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
原因:java.io.FileNotFoundException:example.xls(
(进程无法访问该文件,因为另一进程正在使用该文件))
位于java.io.FileOutputStream.open0(本机方法)
在java.io.FileOutputStream.open(FileOutputStream.java:270)
位于java.io.FileOutputStream。(FileOutputStream.java:213)
位于java.io.FileOutputStream。(FileOutputStream.java:162)
位于hmproject.MenuController.Print(MenuController.java:7985)
... 66多

这是我的代码:

  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{

        }

    }
public void Print()引发JRException、IOException、FileNotFoundException{
HSSFWorkbook=新的HSSFWorkbook();
HSSFSheet电子表格=工作簿.createSheet(“示例”);
HSSFRow行=空;
对于(int i=0;i
我理解这一例外,但我的问题是:

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


我如何在FileOutputStream上进行此测试?

你真的不能,而且它通常基于操作系统

您可以查看此链接以了解更多信息


为了避免此问题,可以在文件名中添加增量。与时间有关的事情。System.currentTimeMillis()。因此,您的文件将永远不会具有相同的名称。当然,您必须不时地删除生成的文件

您不能删除,而且通常是基于操作系统的

您可以查看此链接以了解更多信息

为了避免此问题,可以在文件名中添加增量。与时间有关的事情。System.currentTimeMillis()。因此,您的文件将永远不会具有相同的名称。当然,您必须不时删除生成的文件

我从中找到了答案

但这并不是我想要的,所以我尝试根据自己的需要对其进行修改:

 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");
        File sameFileName = new File(file.getName());

        if (!file.exists()||file.renameTo(sameFileName)) {

            System.out.println("file is closed");
            FileOutputStream out = new FileOutputStream(file);
            workbook.write(out);
            out.close();
            Desktop.getDesktop().open(file);
        } else {

            System.out.println("file is opened");
        }

    }
public void Print()引发JRException、IOException、FileNotFoundException{
HSSFWorkbook=新的HSSFWorkbook();
HSSFSheet电子表格=工作簿.createSheet(“示例”);
HSSFRow行=空;
对于(int i=0;i
我从

但这并不是我想要的,所以我尝试根据自己的需要对其进行修改:

 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");
        File sameFileName = new File(file.getName());

        if (!file.exists()||file.renameTo(sameFileName)) {

            System.out.println("file is closed");
            FileOutputStream out = new FileOutputStream(file);
            workbook.write(out);
            out.close();
            Desktop.getDesktop().open(file);
        } else {

            System.out.println("file is opened");
        }

    }
public void Print()引发JRException、IOException、FileNotFoundException{
HSSFWorkbook=新的HSSFWorkbook();
HSSFSheet电子表格=工作簿.createSheet(“示例”);
HSSFRow行=空;
对于(int i=0;i
我尝试了您的建议,但我将有许多内容相同的文件。您必须实施一种方法来不时删除旧文件。假设您检查excel或libre office是否打开并关闭。或者根据文件名中的时间删除所有文件。并处理打开文件时可能发生的所有异常。您将能够删除所有旧文件,因为用户不会总是打开它们。您也可以使用异常处理要求用户关闭用于锁定生成文件的程序。谢谢,我使用了您为我建议的资源中的答案。我尝试了您的建议,但我将有许多具有相同内容的文件。您必须实现一种不时删除旧文件的方法。假设您检查excel或libre office是否打开并关闭。或者根据文件名中的时间删除所有文件。并处理打开文件时可能发生的所有异常。您将能够删除所有旧文件,因为用户不会总是打开它们。