Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

Java
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)


What I have tried:

I started using threads or something, but it didn't help.
Posted
Updated 14-Oct-21 3:20am
v2
Comments
Richard MacCutchan 14-Oct-21 7:10am    
You need to find out which activity is using the file at the time you try to write to it.

No that I have formatted your code and output correctly the error messages are clearer. Check the code in both of those locations to see why there is a conflict.

1 solution

Look at what you appear to be doing:
Java
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);

One of the first things you do is open a file, apparently to read it. Then you try to move that file to a different folder. You cannot do that. If you open a file, you have to close it before you can move it.
 
Share this answer
 
Comments
[no name] 14-Oct-21 15:07pm    
I understood, thanks very much! Great Answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900