This is my code, which accept generally .xlsx format file and insert to database and also move accept file to my specified folder. When I trying to move it, I have getting this error:
Normally, it successfully writes the received excel file to the database, but when I added the new feature, which move in specified folder, I started getting this error. I started using threads or something, but it didn't help.
public static void acceptExcellFileAndInsertToDatabase(File file) {
try {
String phoneNumber = "";
String textMessage = "";
FileInputStream excelFile = new FileInputStream(file);
Path sourcePath = Paths.get(String.valueOf(file));
Path targetPath = Paths.get("C:\\Users\\anar.memmedov\\Desktop\\ko\\" sourcePath.getFileName());
Files.move(sourcePath, targetPath);
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
phoneNumber = NumberToTextConverter.toText(currentCell.getNumericCellValue());
} else if (currentCell.getCellTypeEnum() == CellType.STRING) {
textMessage = String.valueOf(currentCell.getStringCellValue());
}
}
}
insertExcellFileToDb(phoneNumber, textMessage);
System.out.println(phoneNumber " " textMessage);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
java.nio.file.FileSystemException: C:\Users\anar.memmedov\Desktop\file.xlsx -> C:\Users\anar.memmedov\Desktop\ko\file.xlsx: The process cannot access the file because it is being used by another process.
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:387)
at sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:287)
at java.nio.file.Files.move(Files.java:1395)
at az.expressbank.insertdatabasetheexcellfile.util.ExcellWriteToDatabase.acceptExcellFileAndInsertToDatabase(ExcellWriteToDatabase.java:37)
at az.expressbank.insertdatabasetheexcellfile.test.Test.main(Test.java:29)
CodePudding user response:
You are not closing FileInputStream
at the end.
Try to put this when you don't need the fileInputStream
.
excelFile.close();
CodePudding user response:
You can use try
with resource. It will automatically close your resource:
public static void acceptExcellFileAndInsertToDatabase(File file) {
try(FileInputStream excelFile = new FileInputStream(file)) {
String phoneNumber = "";
String textMessage = "";
Path sourcePath = Paths.get(String.valueOf(file));
Path targetPath = Paths.get("C:\\Users\\anar.memmedov\\Desktop\\ko\\" sourcePath.getFileName());
Files.move(sourcePath, targetPath);
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
phoneNumber = NumberToTextConverter.toText(currentCell.getNumericCellValue());
} else if (currentCell.getCellTypeEnum() == CellType.STRING) {
textMessage = String.valueOf(currentCell.getStringCellValue());
}
}
}
insertExcellFileToDb(phoneNumber, textMessage);
System.out.println(phoneNumber " " textMessage);
}
}