Java gets all files with the specified suffix name under a path

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.