简体   繁体   中英

Issue with program for renaming files

I'm having a problem making a program that reads the files from a folder, stores them in an array, shuffles its elements and then renames them.The idea was shuffling a folder with photos so every time i wanted to view them they would be in a completely different order.

Below is the method i use to randomize the indexes of the file array using util.Random.

public static File[] RandomizeArray(File[] array){
    Random rgen = new Random();  // Random number generator         

    for (int i=0; i<array.length; i++) {
        int randomPosition = rgen.nextInt(array.length);
        File temp = array[i];
        array[i] = array[randomPosition];
        array[randomPosition] = temp;
    }

    return array;
}

And this is my main:

 public static void main(String[] argv) throws IOException {

        String absolutePath = "C:\\Users\\test";
        File dir = new File(absolutePath);
        File[] filesInDir = dir.listFiles();

        RandomizeArray(filesInDir);

        for(int j = 0 ; j < filesInDir.length ; j++) {
            String name = filesInDir[j].getName();
            int p = name.indexOf('.');
            String extension = name.substring(p + 1);
            String newName = Integer.toString(j)+"."+extension;
            String newPath = absolutePath + "\\" + newName;
            filesInDir[j].renameTo(new File(newPath));
            System.out.println(name + " changed to " + newName);
        }

    }
} 

The problem is when i run it for the first time on the folder "test" everything goes smoothly and i get a folder with files like 0.jpg , 1.mpeg, 2.png etc. but the second time there are some files with the same name and instances where, for example, from 12.jpg it goes to 14.jpg and skips 13.

Im sure its something simple but i cant figure out what.

My guess is that it not only skips some numbers, but that it deletes some files actually! When renaming the files, you might end up overwriting some already existing files. Take the following example: You have 3 files: 0.jpg, 1.jpg, 2.jpg. They should be renamed as follows (a possible result of your shuffling):

0.jpg -> 1.jpg
1.jpg -> 2.jpg
2.jpg -> 0.jpg

This would work if all the files would be renamed at the same time. But if they are renamed sequentually, the first renaming ( 0.jpg -> 1.jpg ) will overwrite the "old" 1.jpg. When the "old" 1.jpg should be renamed, it's actually the new 1.jpg (=old 0.jpg ) being renamed to 2.jpg , overwriting the "old" 2.jpg . And then, that file get's renamed again to 0.jpg . So, you'll end up only with 0.jpg (which in this case is actually the same as the original 0.jpg ), but you've overwritten and thus lost the others.

Depending on your shuffling, you might end up overwriting only some of the files, and thus see some numbers skipped.

How to solve it? 2 Variants: 1) move the files to a new directory, and then in the end delete the old directory (it should be empty now, as all files have been moved) and rename the new directory to the name of the original directory. 2) rename the files first to include some prefix, eg "tmp_" + j + "." + extension "tmp_" + j + "." + extension , and after all files have been renamed, rename the files again to remove the prefix.

Both variants make sure that no file is accidentially overwritten.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM