1

In JavaFx. I'm trying to export table-View contents to excel using Apache POI.Well,when i click the button Export in the first time every thing is worked good and the the contents of tableView are exported ,and when i want to open exported file .xls using excel and trying to click again the program invoke this exception :

    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

.

This is my code :

  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{

        }

    }

I understand this exception ,but my question is :

How to confirm if file is used by another process or no ?

How i can make this test on FileOutputStream?

2 Answers2

4

You can't really and it's usually OS based.

You can check this link for more information java-check-if-file-is-already-open

To avoid this problem you could add an increment in the name of the file. Something related to the time. System.currentTimeMillis(). So your file will never have the same name. You will have of course to delete the generated files from time to time

Xavier Bouclet
  • 922
  • 2
  • 10
  • 23
  • I tried what you suggest ,but i will have many files with same content. – Menai Ala Eddine - Aladdin Sep 02 '17 at 15:01
  • You have to implement a way to delete the old files from time to time. Let's say you check if excel or libre office are open and close it. Or delete all the files based on the time in the name of your file. And handle all exception that might happen if the file is opened. You will be able to delete all your old files because the user won't always have them opened. You could also use exception handling to ask your user to close the program witch is used to lock the generated file. – Xavier Bouclet Sep 02 '17 at 15:20
  • Thanks ,i used answer from the resource which you suggested for me. – Menai Ala Eddine - Aladdin Sep 02 '17 at 16:07
1

I found the answer from Check if file is already open

But it is not what i want exactly so i tried to modify it with my need :

 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");
        }

    }