- import java.io.*;
- public class ListFileDemo
- {
- public static void main(String[] args) throws Exception
- {
- File file = new File("D:\\software");// 想要遍历的文件夹
- BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\list.txt"));// 遍历结果的存放位置
- String endstr = ".java";//遍历文件的结束标志
- listFile(file, bw, endstr);
- bw.close();
- }
- public static void listFile(File f, BufferedWriter bw, String endstr)
- throws IOException
- {
- File[] files = f.listFiles();
- for (File file : files)
- {
- if (file.isDirectory())
- {
- listFile(file, bw, endstr);
- }
- else
- {
- if (file.getName().endsWith(endstr))
- {
- bw.write(file.getName());
- bw.newLine();
- }
- }
- }
- }
- }
复制代码
这个应该可以实现你需要的功能 |