Package Filedemo;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileWriter;
Import Java.io.FilenameFilter;
Import java.io.IOException;
Import java.util.ArrayList;
public class Searchsuffixindir {/** * * Prints the file path of all the given suffix names under the given directory and subdirectories to a file. * * Depth traverse the parent directory to save the eligible file path to the collection.
(for filtering).
* * Then write the collection to the file.
* */public static void main (string[] args) {file Dir = new File ("F:\\javatest");
File File = new file (dir, "Suffix.txt");
Internal class FilenameFilter Searchsuffix = new FilenameFilter () {@Override public boolean accept (File dir, String name) {
Return Name.endswith (". Java");
}
};
arraylist<file> path = new arraylist<file> ();
Writetoarr (Dir,searchsuffix,path);
WriteToFile (Path,file);
private static void WriteToFile (arraylist<file> al, file file) {BufferedWriter bw = null;
try {bw = new BufferedWriter (new FileWriter (file)); for (File f:al) {new String ();//Bw.write (STring.valueof (F.length ()));
Bw.write (F.getabsolutepath ());
Bw.newline ();
Bw.flush (); The catch (IOException e) {throw new RuntimeException ("Write failed.
");
}finally{if (BW!= null) try {bw.close (); The catch (IOException e) {throw new RuntimeException ("Shutdown failed.
"); }} public static void Writetoarr (file dir, FilenameFilter Searchsuffix, arraylist<file> al) {file [
]files = Dir.listfiles ();
for (File f:files) {if (F.isdirectory ()) {//recursive.
Writetoarr (F, Searchsuffix, AL);
}else{if (searchsuffix.accept (dir, F.getname ())) {Al.add (f);
}
}
}
}
}