通过下面代码把文件夹的文件以文件夹命名并移动到和文件夹同级目录,因为子文件夹的名字取名无意义,不方便管理.
import java.io.File;
import java.io.FilenameFilter;
import java.nio.file.Files;
import java.util.Scanner;
public class MainTest {
public static void main(String[] args) {
File rootPath= new File("C:\\my\\video\\ue4_1");
if(!rootPath.exists()){
Scanner scan = new Scanner(System.in);
System.out.println("please input file dir:");
while (scan.hasNext()) {// 判断是否还有输入
rootPath=new File(scan.next());
if(rootPath.isDirectory()){
break;
}else{
System.out.println("输入的目录:" +rootPath+"不存在,情继续输入:");
}
}
scan.close();
}
System.out.println(rootPath.list().length);
for (File currentDir : rootPath.listFiles()) {
if(currentDir.isDirectory()){
File[] files = currentDir.listFiles();
if(files!=null&&files.length>0){
if(files.length==1){
File childFile = files[0];
if(childFile.isFile()){
int i = childFile.getName().indexOf(".");
File dest;
if(i>0){
String substring = childFile.getName().substring(i);
dest = new File(rootPath,currentDir.getName() +substring);
}else{
dest = new File(rootPath,currentDir.getName() + childFile.getName());
}
boolean b = childFile.renameTo(dest);
System.out.println("改名"+childFile.getName()+"为"+dest.getAbsolutePath()+"是否成功:"+b);
}else{
System.out.println("忽略子文件:"+childFile);
}
}else{
for (int i = 0; i < files.length; i++) {
File childFile = files[i];
if(childFile.isFile()){
File dest = new File(rootPath,currentDir.getName() + childFile.getName());
boolean b =childFile.renameTo(dest);
System.out.println("改名"+childFile.getName()+"为"+dest.getAbsolutePath()+"是否成功:"+b);
}else{
System.out.println("忽略子文件:"+childFile);
}
}
}
}
}else{
System.out.println("忽略文件:"+currentDir);
}
}
}
}
网友评论