简书链接:关于某bilibli下载器缺陷下载的文件在文件夹子目录修正方法
文章字数:139,阅读全文大约需要1分钟
通过下面代码把文件夹的文件以文件夹命名并移动到和文件夹同级目录,因为子文件夹的名字取名无意义,不方便管理.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| 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); } } } }
|