将.cr2映像从一个文件夹复制到另一个文件夹的程序出现java.nio.file.filesystemexception错误

yeotifhr  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(247)

这个项目的目标是根据拍摄日期来组织这些图像。我什么都要做,但搬家给了我很多麻烦。当我移动它时,它总是适用于前两个图像文件,然后开始给出错误消息:“进程无法访问该文件,因为它正被另一个进程使用。”对于其他进程,我尝试了很多不同的移动方法,它总是移动前两个,而对其他进程不起作用。我做错什么了?
以下是我用于移动器的代码:

// Moves each image file to its respective folder based on the date of creation
private static void moveImages(String mainPath, String[] fileDates, String[] fileNames) 
{
    // Loops through moving each file in the array that contains the dates of each file
    for(int i = 0; i < fileDates.length; i++)
    {
        String fromFile = mainPath + "\\" + fileNames[i];
        String toFile = mainPath + "\\" + directoryName(fileDates[i]) + "\\" + fileNames[i];

        Path source = Paths.get(fromFile);
        Path target = Paths.get(toFile);

        try
        {       
            Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);

            System.out.println("File " + fileNames[i] + " moved successfully"); 
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}

编辑:
我用于此代码的主要方法如下:

public static void main(String[] args) 
{
    //Stores the path name input by the user for future reference
    String mainPath = userInput();

    //Creates an instance of the data that is collected from the images in the mainPath
    GetData data = new GetData();

    //Creates a reference to the collection of the names of all the images in the mainPath 
    String[] names = data.getNames(mainPath);

    //Creates a reference to the collection of the dates of all the images in the mainPath
    String[] dates = data.getDates(mainPath, names);

    //Create a unique directory for each date extracted from images if it doesn't already exist
    createDirectory(mainPath, dates);

    // Move each image to its respectful directory depending on its creation date
    moveImages(mainPath, dates, names);

    // Lets the user know the program is done running 
    System.out.println("End of program");            
}

它打印出来的错误如下:

java.nio.file.FileSystemException: C:\Users\Owner\Desktop\try\IMG_7100.CR2 -> C:\Users\Owner\Desktop\try\Jul 28, 2019\IMG_7100.CR2: The process cannot access the file because it is being used by another process.

at java.base/sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at java.base/sun.nio.fs.WindowsFileCopy.move(Unknown Source)
at java.base/sun.nio.fs.WindowsFileSystemProvider.move(Unknown Source)
at java.base/java.nio.file.Files.move(Unknown Source)
at Organize.moveImages(Organize.java:149)
at Organize.main(Organize.java:29)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题