Java File.renamTo 不工作

Java File.renamTo not working

我编写了将目录中所有 jpg 文件从 1 重命名为 n(文件数)的代码。 如果有 50 个 jpg 文件,在 运行 程序之后,所有文件都重命名为 1.jpg ,2.jpg 等等,直到 50.jpg 但是如果我手动将文件重命名为 50.jpg 到 aaa.jpg 然后再次 运行 程序不会重命名该文件,我将面临问题 我浪费了一天时间来解决这个问题 请帮助我 代码:

public class Renaming {
    private static String path;             // string for storing the path
    public static void main(String[] args) {

        FileReader fileReader = null;           // filereader for opening the file
    BufferedReader bufferedReader = null;   // buffered reader for buffering the data of file
        try{
            fileReader = new FileReader("input.txt");   // making the filereader object and paasing the file name
            bufferedReader = new BufferedReader(fileReader); //making the buffered Reader object
            path=bufferedReader.readLine();
            fileReader.close();
            bufferedReader.close();
        }
        catch (FileNotFoundException e) {           // Exception when file is not found
            e.printStackTrace();
        } 
        catch (IOException e) {                     // IOException
            e.printStackTrace();
        }
        finally {

             File directory=new File(path);

        File[] files= directory.listFiles();        // Storing the all the files in Array

        int file_counter=1;
           for(int file_no=0;file_no<files.length;file_no++){
               String Extension=getFileExtension(files[file_no]);   //getting the filw extension
               if (files[file_no].isFile() && (Extension .equals("jpg")|| Extension.equals("JPG"))){            // checking that if file is of jpg type then apply renaming         // checking thaat if it is file

                File new_file = new File(path+"\"+files[file_no].getName()); //making the new file

                new_file.renameTo(new File(path+"\"+String.valueOf(file_no+1)+".jpg"));   //Renaming the file
               System.out.println(new_file.toString());
                file_counter++;             // incrementing the file counter
            }

        }

        }


}

private static String getFileExtension(File file) {     //utility function for getting the file extension 
    String name = file.getName();
    try {
        return name.substring(name.lastIndexOf(".") + 1);   // gettingf the extension name after . 
    } catch (Exception e) {
        return "";
    }
}`

首先,您应该使用路径分隔符 / 。它适用于 Windows、Linux 和 Mac OS。 这是我将所有文件重命名为文件夹提供的问题的版本。希望这会帮助你。我使用最后一个 JDK 版本来加速和减少代码。

public class App {

    private String path = null;
    public static int index = 1;

    public App(String path){
        if (Files.isDirectory(Paths.get( path ))) {
            this.path = path;
        }
    }

    public void rename() throws IOException{
        if ( this.path != null){
            Files.list(Paths.get( this.path ))
            .forEach( f -> 
            {
                String fileName = f.getFileName().toString();
                String extension = fileName.replaceAll("^.*\.([^.]+)$", "");             

                try {
                    Files.move( f ,Paths.get( this.path + "/" +   App.index + "." + extension));
                    App.index++;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }           
            );
        }
    }

    public static void main(String[] args) throws IOException {
        App app = new App("c:/Temp/");
        app.rename();
    }
}