Java Rename class saves deleted random snapshots at startup

I have a linux operating system, by the way.

import java.io.*;
import java.text.DecimalFormat;
import java.awt.Desktop;

public class rename {
public static boolean renameTo(File Pictures){

    File[] listofFiles = Pictures.listFiles();
    boolean check = false;
    if(listofFiles != null){

        int count = 000;
        for(File pic : listofFiles){
            String filename = pic.getName();
            String extention = filename.substring(filename.lastIndexOf(".")+1, filename.length());
            String pictureExtention = "JPG";
            if(extention.equals(pictureExtention)){
                //we have picture, yay!
                count++;
                pic.renameTo(new File((new DecimalFormat("000").format(count))+".JPG"));
                check=true;
            }//end if

        }//end for

    }//end if
    return check;
}



public static void main(String[] args) throws IOException {
    String homePath = System.getProperty("user.home");
    File home = new File(homePath);
    File pictures = new File(home, "Test");
    Desktop.getDesktop().open(pictures);
    boolean x = renameTo(pictures);
    System.out.println(x);
}

}//end class

Therefore, by renaming many JPG files, it will delete some from the file, and I cannot understand why. Any ideas? I want him to rename the file 001.JPG (say 25 images) 025.JPG. However, it correctly renames those that are not deleted.

+4
source share
2 answers

The class method can return its files in any order. FilelistFiles

There is no guarantee that name strings in the resulting array will be displayed in any particular order; they, in particular, are not guaranteed in alphabetical order.

, , . "005.JPG" "006.JPG" , "006.JPG" .

, , - .

Arrays.sort(files, Comparator.comparing(File::getName).reversed());
+4

, , . , , , , , .

, 001.JPG, abc.JPG 001.JPG

, , .

0

Source: https://habr.com/ru/post/1629618/


All Articles