【问题标题】:Rename all files in a folder using java [duplicate]使用java重命名文件夹中的所有文件[重复]
【发布时间】:2014-08-03 16:19:31
【问题描述】:

我们如何使用 java 重命名文件夹中的所有文件?

C:/temp/pictures/1.jpg
C:/temp/pictures/2.jpg
C:/temp/pictures/3.jpg
C:/temp/pictures/4.jpg
C:/temp/pictures/5.jpg

重命名为

C:/temp/pictures/landscape_1.jpg
C:/temp/pictures/landscape_2.jpg
C:/temp/pictures/landscape_3.jpg
C:/temp/pictures/landscape_4.jpg
C:/temp/pictures/landscape_5.jpg

亲切的问候

【问题讨论】:

标签: java file directory rename


【解决方案1】:

查看以下代码,检查文件夹中的文件并重命名。

File dir = new File("D:/xyz");

if (dir.isDirectory()) { // make sure it's a directory
    for (final File f : dir.listFiles()) {
        try {
            File newfile =new File("newfile.txt");

            if(f.renameTo(newfile)){
                System.out.println("Rename succesful");
            }else{
                System.out.println("Rename failed");
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2017-10-02
    • 1970-01-01
    • 2018-04-07
    • 2020-06-09
    • 2013-04-24
    • 2012-08-10
    • 2020-05-17
    相关资源
    最近更新 更多