如何在java中重命名文件夹
我的文件夹结构看起来像如何在java中重命名文件夹,java,directory,rename,Java,Directory,Rename,我的文件夹结构看起来像 D: |- Folder1 |- File1 |- Folder2 |- File2 输出: D: |- Directory1 <- renamed |- File1 |- Directory2 <- renamed |- File2 D: |-下面是我解决问题的方法 我得到指定深度的目录 创建名称已更改的新目录 使用FileUtils()将文件复制到新文件夹 手动删除所有旧文件夹 package com.so.pr
D:
|- Folder1
|- File1
|- Folder2
|- File2
输出:
D:
|- Directory1 <- renamed
|- File1
|- Directory2 <- renamed
|- File2
D:
|-下面是我解决问题的方法
我得到指定深度的目录
创建名称已更改的新目录
使用FileUtils()将文件复制到新文件夹李>
手动删除所有旧文件夹
package com.so.practice;
导入java.io.File;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入org.apache.commons.io.FileUtils;
公共类软件{
公共静态void main(字符串[]args)引发IOException{
List files=getDirs(新文件(“根文件夹的路径”),0);//级别0位于父级、级别1内,依此类推
System.out.println(文件);
字符串[]路径=新字符串[files.size()];
int i=0;
用于(文件:文件){
路径[i++]=file.getAbsolutePath();
}
字符串matchword=“Folder1”;
//文件f=null;
HashMap old_new=新HashMap();
对于(int j=0;j”+旧路径);
}
//f=新文件(路径[j]);
//f、 mkdirs();
}
for(字符串键:old_new.keySet()){
copyDirectory(新文件(键),新文件(旧文件(键));
}
//copyDirectory(新文件(old_new.get)、新文件(arg0));
}
静态列表getDirs(文件父级,int级){
List dirs=new ArrayList();//存储
对于(文件f:parent.listFiles()){
if(f.isDirectory()){
如果(级别==0)直接相加(f);
其他的
if(level>0)dirs.addAll(getDirs(f,level-1));//递归
}
}
返回目录;
}
}
使用IDE并重构您的代码/结构我已经用nio尝试过了。但停在一个层面是复杂的。如果没有nio,我不知道如何控制级别。你使用什么样的IDE?你已经问了其他问题如何在java中重命名文件/文件夹,它也有一个工作代码。但在on中抛出了一个AccessDeniedException
。那么这真的是一个与java相关的问题吗?您可以使用其他工具重命名特定文件夹吗?@DineshRavi因此您可以手动重命名导致问题的其中一个文件吗?避免使用System.exit(0)代码>如果需要创建非基于操作系统的路径,而不是文件newDir=newfile(dir.getParent()+“\”+newDirName);使用File newDir=newfile(dir.getParent()+File.separator+newDirName);你知道目标目录中的现有文件已经存在时会被覆盖吗?@devpuh谢谢,我不知道
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);
}
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;
}
}