- package test1;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- /*
- * 包含txt的文件名写入txt文本中
- *
- * */
- public class day22 {
- public static void main(String[] args) throws IOException {
-
- File file = new File("E:\\java11");
- ArrayList<File> al = new ArrayList<File>();
-
- File dest = new File("D:\\javainfo.txt");
-
- add(file,al);//将含有java的文件存入集合中
-
- list(al,dest);//遍历集合,将文件名写入文本中
-
- }
- private static void list(ArrayList<File> al, File dest) throws IOException {
- // TODO Auto-generated method stub
-
- BufferedWriter bufw = new BufferedWriter(new FileWriter(dest));
-
- for(File f : al)
- {
- String path = f.getAbsolutePath();
- bufw.write(path);
- bufw.newLine();
- bufw.flush();
- }
-
- bufw.close();
-
- }
- private static void add(File file, ArrayList<File> al) {
- // TODO Auto-generated method stub
- File[] files = file.listFiles();
- for(File f :files)
- {
- if(f.isDirectory())
- {
- add(f,al);
- }else{
- if(f.getName().endsWith(".java"))
- al.add(f);
-
- }
-
- }
-
- }
- }
复制代码
我写了一个,你参考一下。 |