Java实现查找电脑盘符中遗忘的 “视频” ^-^

是否还在为有好的小电影下载在电脑中因为忘了位置而苦恼?不要慌,陈哥教你一招,Java代码实现电脑盘符找片儿。

具体实现代码

package com.io.file;

import java.io.File;

/**
 * @author Oxygen
 *
 */
public class Demo_GetFile {
	/**
	 * 记录文件个数
	 */
	private static int sum = 0;

	public static void main(String[] args) {
		// 要查找的路径
		String path = "E:\\";
		// 计时开始
		long start = System.currentTimeMillis();
		int sum = GetAllDirAndFileName(path);
		long end = System.currentTimeMillis();
		// 计时结束
		// 得到结果
		System.out.println("指定目录  " + path + "  下共找到视频  [支持格式 .avi  .mp4  .fly ]  " + sum + "个  总耗时:" + (end - start)
				+ "ms     " + (end - start) / 1000 + " s");
	}

	/**
	 * 
	 * @param path 找片儿路径
	 * @return 找片儿的方法
	 */
	public static int GetAllDirAndFileName(String path) {
		int index = 1;
		File f = new File(path);
		String[] list = f.list();// 拿到当前目录下的文件列表集
		String filePath;
		if (list != null) {
			for (String str : list) {
				filePath = path + "\\" + str;
				File f1 = new File(filePath);
				if (f1.isDirectory() && !f1.isFile()) {// 如果是一个目录,那么继续往该目录下找
					GetAllDirAndFileName(filePath);
				} else {
					if (str.endsWith(".avi") || str.endsWith(".mp4") || str.endsWith(".flv")) {// 支持的格式 .avi .mp4 .flv
						index++;
						System.out.println(path);
						System.out.println("  " + str);
					}
				}
			}
		}
		sum += index;
		return sum;
	}
}

运行结果

Java实现查找电脑盘符中遗忘的 “视频” ^-^