今天下载了好多视频文件都放在了F:盘中,因为F盘的视频文件比较多,并且存放的每一个文件夹中都有,因而我就想把全部的视频文件从新整理放到一个新的文件夹中。
因为本人比较懒,不想手动操做,感受本身的动手的话操做的步骤繁多,并且还不必定能把全部的视频文件都给找出来。
懒人定有懒人的作法,因而我就写了一段代码,让代码替我实现,很少说了,直接贴代码,相信你们均可以看懂的。
我以F:/迅雷下载/javamail文件夹为例,若是想换路径,直接修改oldpath就能够了
个人全部视频文件都是 .avi结尾,若是想换.jpg .mp3 什么的本身修改contains java
package com.move; import java.io.File; public class MoveFiles { static String oldpath = "F:"+File.separator+"迅雷下载"+File.separator+"javamail"; static String newpath = "f:"+File.separator+"file"+File.separator; static String contains = ".avi"; public static void main(String[] args){ File filePath = new File(oldpath); if (filePath.exists()) { showAllFiles(filePath); System.out.println("success"); } else { System.out.println("error"); } } final static void showAllFiles(File dir) { File[] fs = dir.listFiles(); for (int i = 0; i < fs.length; i++) { String str = fs[i].getAbsolutePath(); if (str.contains(contains)) { File oldFile = new File(str); File fnewpath = new File(newpath); if (!fnewpath.exists()) fnewpath.mkdirs(); File fnew = new File(newpath + oldFile.getName()); oldFile.renameTo(fnew); } if (fs[i].isDirectory()) { try { showAllFiles(fs[i]); } catch (Exception e) { e.printStackTrace(); } } } } }