JAVA遍歷系統中所有的文件(源碼)

import java.io.File;

 

public class FileTest {

 

  private static int fileCount = 0;

//遍歷盤符中所有的文件

  private static void findAllFiles(File file) {

    if (!file.isDirectory()) {

      System.out.println(file.getName());

      fileCount++;

    } else {

      File[] childFile = file.listFiles();

      if (childFile != null)

        for (int i = 0; i < childFile.length; i++) {

          findAllFiles(childFile[i]);

        }

    }

    System.out.println(file.getName() + "has" + fileCount + "files");

  }

 

  public static void main(String[] args) {

//獲得系統中所有的盤符

    File[] roots = File.listRoots();

    for (int i = 0; i < roots.length; i++) {

      File file = roots[i];

      findAllFiles(file);

    }

  }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章