Method One:
http://blog.csdn.net/zjx102938/article/details/8114114
Import Java.io.File; Import java.util.ArrayList; Import java.util.List; public class FileFind {public static void Main (string[] args) { String path = "D:\\test"; List data=new ArrayList (); Data=getdata (path, data); for (int i=0;i<data.size (); i++) { System.out.println (Data.get (i)); } } private static list GetData (String path, list data) { file F=new file (path); if (F.isdirectory ()) { file[] fs=f.listfiles (); for (int i=0;i<fs.length;i++) { data=getdata (Fs[i].getpath (), data); } } else if (F.getname (). EndsWith (". Java")) { Data.add (F.getname ()); } return data; } }
Method Two:
Package Test;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 {private static String path = "E:\\myeclipse\\mirror_parse\\webroot\\mirror"; /** * * Print the file path of all the given suffix names under the given directory and subdirectories to a file. * * Deep 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 (path); File File = new file (dir, "Suffix.txt"); Inner class FilenameFilter Searchsuffix = new FilenameFilter () {@Override public boolean accept (Fil e dir, String name) {return Name.endswith (". html"); } }; 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 (); }} catch (IOException e) {throw new RuntimeException ("Write Failed! "); }finally{if (bw! = null) try {bw.close (); } 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 ())) {System.out.println (f); Al.add (f); } } } } }
Java gets all files with the specified suffix name under a path