這是我的代碼,它通常接受 .xlsx 格式的檔案并插入到資料庫并將接受檔案移動到我指定的檔案夾。當我嘗試移動它時,出現此錯誤:
通常,它成功地將接收到的 excel 檔案寫入資料庫,但是當我添加新功能時,在指定檔案夾中移動時,我開始收到此錯誤。我開始使用執行緒或其他東西,但它沒有幫助。
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)
uj5u.com熱心網友回復:
你FileInputStream最終沒有關閉。
當您不需要fileInputStream.
excelFile.close();
uj5u.com熱心網友回復:
您可以try與資源一起使用。它會自動關閉您的資源:
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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/318439.html
