如何獲取指定目錄包含的文件和子目錄
如何獲取指定文件的基本信息
FileInfo
FileInfo
FileInfo
FileInfo
FileInfo
FileInfo
FileInfo
FileInfo
FileInfo
遍歷文件夾
文件夾是樹形結構
區別
public static void searchFile(String path
File file = new File(path);
if (file
LinkedList<File[]> levelLinked = new LinkedList<File[]>();
levelLinked
do {
File[] childFiles = levelLinked
for (File cf : childFiles) {
if (cf
// 此處控制遍歷的方向
levelLinked
// levelLinked
} else {
String fileName = cf
String filePath = cf
String fileParent = cf
// 比較算法
String[] sf = { fileName
resultList
}
}
} while (levelLinked
}
}
此處加入一些API提供的工具類
String[] hz = {java
Arrays
Arrays
如果明確目錄的層次結構簡單
個人覺得
////////////////////////
采用遞歸的方式遍歷
public void FindFile(string dirPath) //參數dirPath為指定的目錄
{
//在指定目錄及子目錄下查找文件
DirectoryInfo Dir=new DirectoryInfo(dirPath);
try
{
foreach(DirectoryInfo d in Dir
{
FindFile(Dir+d
listBox
}
foreach(FileInfo f in Dir
{
listBox
}
}
catch(Exception e)
{
MessageBox
}
}
用下面代碼限制文件的類型
foreach(FileInfo f in Dir
“*
///////////////////////////////
代碼改成如下
DirectoryInfo TheFolder=new DirectoryInfo(folderFullName);
DirectoryInfo[] dirInfo = TheFolder
//遍歷文件夾
foreach(DirectoryInfo NextFolder in dirInfo)
this
FileInfo[] fileInfo = TheFolder
//遍歷文件
foreach(FileInfo Next
From:http://tw.wingwit.com/Article/program/net/201311/14311.html