参考了一下jdk和之前写过的东西。课堂整理了下
package qgm;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Reach {
public static void main(String[] args) throws IOException {
File file=new File("E:\\");
List<String> list=new ArrayList<String>();
String FileName="filelist.dat";
scanFile(file, FileName, list);
judgeFile(list);
}
public static void scanFile(File file,String FileName,List<String>list) throws IOException {
File[] files = file.listFiles();//返回File类
if(files != null)
{
for(File f : files)
{
if(f.getName().indexOf(FileName) >= 0)
{
list.add(f.getAbsolutePath());
}
if(f.isDirectory())
{
scanFile(f, FileName, list);
}
}
}
}
public static void judgeFile(List<String> list) {
if (list.size() == 0)
{
System.out.println("没有找到文件。");
} else
{
for(String s : list)
{
System.out.println("找到文件" + s);
}
System.out.println("共有文件" + list.size() + "个");
}
}
}