java移動文件到另外一個文件夾中

今天下載了好多視頻文件都放在了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();
				}
			}
		}
	}
}
相關文章
相關標籤/搜索