java - 使用java重命名文件夹中的所有文件

标签 java file directory rename

<分区>

我们如何使用 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

亲切的问候

最佳答案

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

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();
        }

希望对你有帮助

关于java - 使用java重命名文件夹中的所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24199679/

相关文章:

python - 将文件中的不同列附加到不同的列表?

java - 缓冲输出写入文件错误

android - 检查android的sdcard上是否存在目录

python - Django 目录在哪里创建?

java - 在数组中打印用户输入显示为 null

java - 如何修改此示例代码以显示在 BlackBerry 中更新 UI 的三种方法之间的差异

file - 从VHDL文件中的单行读取不同的数据

Python:获取目录下所有文件和文件夹的列表,创建时间,最后修改时间。系统独立解决方案?

java - java生成唯一id(不超过64位)

java - Android RecyclerView : Swipe between cards in RecyclerView