📜  如何使用Java重命名文件夹的所有文件?

📅  最后修改于: 2022-05-13 01:54:46.065000             🧑  作者: Mango

如何使用Java重命名文件夹的所有文件?


通常,当将文件从相机文件夹传输到我们想要分析图片的工作区时,处理长文件并在通过代码测试时一次又一次地输入它们变得很困难。此外,文件数量可能太大而无法手动重命名每个文件。因此,自动化重命名过程成为必要。

例子:

Input : Read 50 files from the folder 
"C:\Users\Anannya Uberoi\Desktop\myfolder":
Snapshot 1 (12-05-2017 11-57).png
Snapshot 2 (12-05-2017 11-57).png
Snapshot 3 (12-05-2017 11-57).png
Snapshot 4 (12-05-2017 11-57).png   and so on.

Output :Renamed to
1.png
2.png
3.png
4.png   and so on.

// Java program to illustrate
// how to rename Multiple Files
// together using single program
import java.io.File;
import java.io.IOException;
  
public class rename
{
    public static void main(String[] argv) throws IOException
    {
        // Path of folder where files are located
        String folder_path =
               "C:\\Users\\Anannya Uberoi\\Desktop\\myfolder";
  
        // creating new folder
        File myfolder = new File(folder_path);
  
        File[] file_array = myfolder.listFiles();
        for (int i = 0; i < file_array.length; i++)
        {
  
            if (file_array[i].isFile())
            {
  
                File myfile = new File(folder_path +
                         "\\" + file_array[i].getName());
                String long_file_name = file_array[i].getName();
                String[] tokens = long_file_name.split("\\s");
                String new_file_name = tokens[1];
                System.out.println(long_file_name);
                System.out.print(new_file_name);
  
                // file name format: "Snapshot 11 (12-05-2017 11-57).png"
                // To Shorten it to "11.png", get the substring which
                // starts after the first space character in the long
                // _file_name.
                myfile.renameTo(new File(folder_path +
                             "\\" + new_file_name + ".png"));
            }
        }
    }
}

输出:

The files get renamed successfully.

注意:双斜杠 (\\) 是必需的,因为其中一个反斜杠 (\) 充当转义字符,另一个反斜杠 (\) 表示目录更改。