日期:2014-05-20 浏览次数:22161 次
public static void main(String[] args) throws Exception
{
Map<String,Integer> map = new HashMap<String,Integer>();
searchFile(map,new File("e:"));
Set set = map.entrySet();
for(Iterator i = set.iterator();i.hasNext();)
{
Map.Entry<String, Integer> temp = (Map.Entry<String, Integer>)i.next();
System.out.println(temp.getKey() + ":" + temp.getValue());
}
}
public static void searchFile(Map map,File file) throws Exception
{
if(file.isFile())
{
if(file.getName().lastIndexOf(".") != -1)
{
String fileName = file.getName().toLowerCase();
String suffix = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
if(map.containsKey(suffix))
{
map.put(suffix, Integer.parseInt(map.get(suffix).toString()) + 1);
}
else
{
map.put(suffix, 1);
}
}
}
else
{
File[] files = file.listFiles();
for(File tempFile:files)
{
searchFile(map,tempFile);
}
}
}