0

该计划的目标是根据拍摄日期组织图像。我得到了一切工作,但搬家给我带来了很多问题。当我移动它时,它总是适用于前 2 个图像文件,然后开始给出错误消息:“该进程无法访问该文件,因为它正被另一个进程使用。” 在其他人身上,我尝试了很多不同的移动方式,它总是移动前两个,而对其余的则不起作用。我究竟做错了什么?

这是我用于移动器的代码:

// 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)
4

0 回答 0