java 如何在java中重命名文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48339967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 10:02:20  来源:igfitidea点击:

How to rename the folder in java

javadirectoryrename

提问by Dinesh Ravi

My folder structure looks like

我的文件夹结构看起来像

D:
|- Folder1
   |- File1
|- Folder2
   |- File2 

output:

输出:

D:
|- Directory1   <- renamed
   |- File1 
|- Directory2   <- renamed
   |- File2 

The question is how to rename the folders one level down?

问题是如何将文件夹重命名一级?

采纳答案by Dinesh Ravi

Here is how I solved my problem.

这是我如何解决我的问题。

  1. I get the directory present at specified depth.
  2. Create new directory with altered name
  3. Used FileUtils (Apache Commons IO) to copy the files to the new folder.
  4. Manually delete all those old folder.
  1. 我得到指定深度的目录。
  2. 使用更改的名称创建新目录
  3. 使用 FileUtils ( Apache Commons IO) 将文件复制到新文件夹。
  4. 手动删除所有那些旧文件夹。


package com.so.practice;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.io.FileUtils;

public class Sof {
    public static void main(String[] args) throws IOException {
        List<File> files = getDirs(new File("path to root folder"), 0); //level 0 is inside parent , level 1, and so on
        System.out.println(files);
        String[] paths = new String[files.size()];

        int i = 0;
        for (File file : files) {
            paths[i++] = file.getAbsolutePath();
        }
        String matchword = "Folder1";

        //File  f = null;
        HashMap<String, String > old_new = new HashMap<>();
        for (int j = 0; j < paths.length; j++) {
            System.out.println(paths[j]);
            String old_path = paths[j];
            String foldername = new File(paths[j]).getName();
            //02_PA__OPCON.MES.GC.Configuration 
            //02_Configuration

            if(old_path.contains(matchword)){
                paths[j] =paths[j].replaceAll(matchword, "Directory");

                old_new.put(old_path, paths[j]);

            }else{
                System.out.println("skipping->"+old_path);
            }
            //f = new File(paths[j]);
            //f.mkdirs();
        }

        for(String key : old_new.keySet()){
            FileUtils.copyDirectory(new File(key), new File(old_new.get(key)));
        }

        //FileUtils.copyDirectory(new File(old_new.get), new File(arg0));
     }

     static List<File> getDirs(File parent, int level){
        List<File> dirs = new ArrayList<File>(); //to store 
        for(File f: parent.listFiles()){
            if(f.isDirectory()) {
                if (level==0) dirs.add(f);
                else 
                    if (level > 0) dirs.addAll(getDirs(f,level-1)); //recursion
            }
        }
        return dirs;
    }
}

回答by Ann

File dir = new File(dirPath);
if (!dir.isDirectory()) {
  System.err.println("There is no directory @ given path");
} else {
    System.out .println("Enter new name of directory(Only Name and Not Path).");
    String newDirName = scanner.nextLine();
    File newDir = new File(dir.getParent() + "\" + newDirName);
    dir.renameTo(newDir);
}