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?