Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Java 遍歷目錄下所有文件的實作方法

Java 工具類中提供了一個用於管理檔案系統的類別,也就是 File 類。與其他串流類別不同,串流類關注的是文件的內容,而 File 類關注的是磁碟上文件的存放。

    一、File 類有多個建構器,常用的有:

1、public File(String pathname){}

  在 pathname 路徑下建立文件物件

2、public File(String path,String name){}

  在 path 參數指定的目錄中建立具有給定名字的 File 物件,如果 path 為 null,建構器將使用目前目錄建立物件

3、public File(File dir, String name){}

  File 物件 dir 表示一個目錄,在 dir 參數指定的目錄中建立具有給定名字的 File 物件,如果 dir 為 null,

建構器將使用目前目錄建立物件

    二、獲得文件的權限屬性:

1、表明文件是否可讀、可寫、可執行

  boolean canRead()

  boolean canWrite()

  boolean canExecute()

2、設置文件的可讀、可寫、可執行

  boolean setReadable(bollean state,bollean ownerOnly)

  boolean setWritable((bollean state,bollean ownerOnly)

  boolean setExecutable((bollean state,bollean ownerOnly)

3、刪除文件

  boolean delete()

  如果文件被刪除則返回 true,否則返回 false

  void deleteOnExit()

  在虛擬機關閉時將文件刪除

4、判斷文件是否存在

  boolean exists()

5、獲得文件路徑名

  String getCanonicalPath()

  返回包含這個文件的規范路徑名的字符串,該方法會使用恰當的目錄分隔符,並可以獲得底層檔案系統所選擇的大小寫處理方式

  String getName()

  返回包含這個 File 物件的文件名的字符串,但不包含路徑資訊

6、判斷 File 為文件還是目錄

  boolean isDirectory()

  判斷是否為一個目錄

  boolean isFile()

  判斷是否為一個文件

7、獲得 File 物件包含的文件名和目錄名

  String[] list()

  返回這個 File 物件包含的文件名和目錄名構成的字符串陣列

  String[] list(FilenameFilter filter)

  返回有這個 File 物件包含的滿足過濾器條件的文件名和目錄名構成的字符串陣列

File 還有許多方法屬性,更多的可以查看 API 文檔。

現在,使用 File 類來遍歷一個目錄下的所有文件,我的程式過程為:

1、獲取 pathName 的 File 物件

2、判斷該文件或目錄是否存在,不存在時在控制台輸出提醒

3、判斷如果不是一個目錄,就判斷是不是一個文件,是文件則輸出文件路徑

4、獲取此目錄下的所有文件名與目錄名的字符串陣列

5、如果是一個目錄,搜索深度 currentDepth+1,輸出目錄名後,進行遞歸

6、如果是文件,則直接輸出文件名

程式如下:

import java.io.File;
import java.io.IOException;

public class DirErgodic {

 private static int depth=1;
 
 public static void find(String pathName,int depth) throws IOException{
  int filecount=0;
  //獲取pathName的File對象
  File dirFile = new File(pathName);
  //判斷該文件或目錄是否存在,不存在時在控制台輸出提醒
  if (!dirFile.exists()) {
   System.out.println("do not exit");
   return ;
  }
  //判斷如果不是一個目錄,就判斷是不是一個文件,時文件則輸出文件路徑
  if (!dirFile.isDirectory()) {
   if (dirFile.isFile()) {
    System.out.println(dirFile.getCanonicalFile());
   }
   return ;
  }
 
  for (int j = 0; j < depth; j++) {
   System.out.print("  ");
  }
  System.out.print("|--");
  System.out.println(dirFile.getName());
  //獲取此目錄下的所有文件名與目錄名
  String[] fileList = dirFile.list();
  int currentDepth=depth+1;
  for (int i = 0; i < fileList.length; i++) {
   //遍歷文件目錄
   String string = fileList[i];
   //File("documentName","fileName")是File的另一個構造器
   File file = new File(dirFile.getPath(),string);
   String name = file.getName();
   //如果是一個目錄,搜索深度depth++,輸出目錄名後,進行遞歸
   if (file.isDirectory()) {
    //遞歸
    find(file.getCanonicalPath(),currentDepth);
   }else{
    //如果是文件,則直接輸出文件名
    for (int j = 0; j < currentDepth; j++) {
     System.out.print("  ");
    }
    System.out.print("|--");
    System.out.println(name);
   
 }
  }
 
 public static void main(String[] args) throws IOException{
  find("D:\\MongoDB", depth);
 }
}

測試截圖:

Copyright © Linux教程網 All Rights Reserved