All files in the Java directory inquiries (including subdirectories)

List of figures:

Method Code:

  / ** 
     * read all files under the directory 
     * 
     * @param dir 
     * directory 
     * @param fileNames 
     * Save the file name of the set 
     * @return 
     * / 
    public  static  void findFileList (File dir, List <String> fileNames) {
         IF ( !! dir.exists () || dir.isDirectory ()) { // whether or not directory 
            return ; 
        } 
        String [] files = dir.list (); // read the directory information of all directory files 
        for ( int 0 = I; I <files.length; I ++) { // cycle, adding a file name or callback itself
            File = File new new File (the dir, Files [I]);
             IF (file.isFile ()) { // if the file 
                fileNames.add (the dir + "\\" + file.getName ()); // add files full pathname 
            } the else { // If the directory 
                findFileList (File, fileNames); // callback own research 
            } 
        } 
    }

Test code:

public static void main(String[] args) throws Exception {
      List<String> fileNames = new ArrayList<String>();
      FileUtil.findFileList(new File("F:\\HTML\\bootstrap"),fileNames );
      System.out.println();
      for (String value :  fileNames) {
         System.out.println("file:"+value);
      }   
 }

Entire class FileUtil.java:

public class FileUtil {
 
    public static void main(String[] args) throws Exception {
         List<String> fileNames = new ArrayList<String>();
         FileUtil.findFileList(new File("F:\\HTML\\bootstrap"),fileNames );
         System.out.println();
         for (String value :  fileNames) {
            System.out.println("file:"+value);
         }
         
    }
    
    
    /**
     * 读取目录下的所有文件
     * 
     * @param dir
     *            目录
     * @param fileNames 
     * Save the file name of the set 
     * @return 
     * / 
    public  static  void findFileList (File dir, List <String> fileNames) {
         IF (! dir.exists () ||! dir.isDirectory ()) { // judge whether there is a directory 
            return ; 
        } 
        String [] files = dir.list (); // read all the directory information in the directory file 
        for ( int I = 0; I <files.length; I ++) { // cycle, add files own name or callback 
            file file = new new file (the dir, files [I]);
             IF (file.isFile ()) { // if the file
                fileNames.add (dir + "\\" + file.getName ()); // add the full pathname of the file 
            } the else { // If the directory 
                findFileList (File, fileNames); // callback own research 
            } 
        } 
    } 
}

Results Figure:

Guess you like

Origin www.cnblogs.com/henuyuxiang/p/11608997.html