Java遍歷一個文件夾下的全部文件


    Java工具中為我們提供了一個用於管理文件系統的類,這個類就是File類,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);
	}
}

測試截圖:







免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM