案例:整理视频文件夹
需求说明

思路
1.先把文件夹中所有MP4的文件读取出来
2.分别取目录的名字+ ”-“ + 文件名
3.整理后拷贝到其他的文件夹:output文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| File file = new File("E:/test"); File[] dirs = file.listFiles(); for(File dir : dirs) { for(File fileMp4 : dir.listFiles()) { String filePath = fileMp4.getPath(); filePath = filePath.replace("\\","/"); String[] strs = filePath.split("/"); String folderName = strs[strs.length-2]; String fileName = strs[strs.length-1]; String outputFileName = "e:/temp/output/"+folderName+"-"+fileName; byte[] bytes = IOHelpers.readAllBytes(fileMp4); IOHelpers.writeAllBytes(outputFileName,bytes); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| String[] strs = IOHelpers.getFilesRecursively("D:/JavaTest1","mp4");
for (String str:strs) { String strs1 = str.replace("\\","/"); String[] strs2 = strs1.split("/"); String name = strs2[strs2.length-2]; String filename = strs2[strs2.length-1]; String outputFileName = "D:/JavaTest1/output/"+name+"-"+filename; byte[] bytes = IOHelpers.readAllBytes(str); IOHelpers.writeAllBytes(outputFileName,bytes); }
|
练习

思路
1.先找出文件夹下的mp3歌曲
2.用、去分割歌曲名称
3.然后用-去分为两半,前面是歌手的名字去作为文件夹,后面作为歌曲的名字,放在文件夹之下
4.然后读取对应的内容,拷贝到新文件夹中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| String[] strs= IOHelpers.getFilesRecursively("E:/KwDownload/song","mp3");
for (String fileMp3:strs) { String filePath = fileMp3.replace("\\","/"); String[] strs2 = filePath.split("/"); String filename = strs2[strs2.length-1]; String [] strs3 =filename.split("、"); String newfilename = strs3[strs3.length-1]; String [] strs4 =newfilename.split("-"); String name = strs4[strs4.length-2]; String MP3 = strs4[strs4.length-1]; String outputFileName = "D:/JavaTest1/output/"+name+"/"+MP3; byte[]bytes = IOHelpers.readAllBytes(fileMp3); IOHelpers.writeAllBytes(outputFileName,bytes); }
|