3

Getting: java.nio.file.filesystemexception: C:\users\anar.memmedov\desktop\file....

 2 years ago
source link: https://www.codeproject.com/Questions/5315155/Getting-java-nio-file-filesystemexception-C-users
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Getting: 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 anot

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.

Expand ▼   Copy Code
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();
    }
}
Copy Code
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.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK