没有废话只有代码,复制粘贴即可使用
package com.lfs;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class GetAllFile {
private static Map> map = new HashMap();
//获得指定目录下所有文件
public static void main(String[] args) {
File file = new File("F:");
findPath(file);
for(Map.Entry> each : map.entrySet()){
Map temp = each.getValue();
String path = null;
Long mem = 0L;
for(Map.Entry m : temp.entrySet()){
path = m.getKey();
mem = m.getValue();
}
mem = mem / (1024*1024);//返回的文件大小单位是Byte,即字节,需要换算成MB,对应关系为1024B = KB,1024KB = 1MB
if(mem > 300)
System.out.println("t大小:t"+ mem + "MB"
+"tttt文件名:t"+ each.getKey() + "tttt路径:t"+ path);
}
}
//递归寻找
public static void findPath(File f){
if(f != null){
//获得文件夹下面的所有文件和文件并且返回File数组
File[] files = f.listFiles();
if(files != null){
for(File file : files) {
if (file.isFile()) {//如果是文件那么就操作
Map temp = new HashMap();
temp.put(file.getAbsolutePath(), file.length());//存入绝对文件路径和文件大小
map.put(file.getName(), temp);//文件名和封装好的map对象
} else {
//不是文件那么说明是文件夹,继续递归往后找
if(file != null)
findPath(file);
}
}
}
}
return ;
}
}