天天看點

周遊檔案Java中周遊出指定目錄中的所有檔案

在改章節中,我們主要介紹周遊檔案的内容,自我感覺有個不錯的建議和大家分享下

    以下中周遊出項目根目錄中的全部txt檔案,如果你設定了包名test.data,須要周遊指定包名下的全部檔案是,須要把路徑System.getProperty("user.dir")改為System.getProperty("user.dir"+\\test\\data)

    每日一道理

“一年之計在于春”,十幾歲的年紀,正是人生的春天,别辜負了歲月老人的厚愛與恩賜。行動起來,播種夢想吧!

import java.io.File;
import java.io.IOException;
import java.util.Vector;

public class Filepath {

	public static void main(String[] args) throws IOException {
		Vector<String> vecFile = GetTestXlsFileName(System.getProperty("user.dir"));
		for (int i = 0; i < vecFile.size(); i++) {
			System.out.println(vecFile.get(i));
		}
	}

	public static Vector<String> GetTestXlsFileName(String fileAbsolutePath) {
		Vector<String> vecFile = new Vector<String>();
		File file = new File(fileAbsolutePath);
		File[] subFile = file.listFiles();

		for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {
			// 判斷是否為檔案夾
			if (!subFile[iFileLength].isDirectory()) {
				String fileName = subFile[iFileLength].getName();
				// 判斷是否為.txt結尾
				if (fileName.trim().toLowerCase().endsWith(".txt")) {
					vecFile.add(fileName);
				}
			}
		}
		return vecFile;
	}
}      

    結果:

    aaaa.txt