public void traverseFolder2(String path) {
File file = new File(path)
if (file.exists()) {
File[] files = file.listFiles()
if (null == files || files.length == 0) {
System.out.println("文件夹是空的!")
return
} else {
for (File file2 : files) {
if (file2.isDirectory()) {
System.out.println("文件夹:" + file2.getAbsolutePath())
traverseFolder2(file2.getAbsolutePath())
} else {
System.out.println("文件:" + file2.getAbsolutePath())
}
}
}
} else {
System.out.println("文件不存在!")
}
}
扩展资戚举料
public void traverseFolder1(String path) {
int fileNum = 0, folderNum = 0
File file = new File(path)
if (file.exists()) {
LinkedList<File>list = new LinkedList<File>()
File[] files = file.listFiles()
for (File file2 : files) {
if (file2.isDirectory()) {
System.out.println("文件夹:" + file2.getAbsolutePath())
list.add(file2)
foldeNum++
} else {
System.out.println("文件:" + file2.getAbsolutePath())
fileNum++
}
}
File temp_file
while (!list.isEmpty()) {
temp_file = list.removeFirst()
files = temp_file.listFiles()
for (File file2 : files) {
if (file2.isDirectory()) {
System.out.println("文件夹:" + file2.getAbsolutePath())
list.add(file2)
folderNum++
} else {
System.out.println("文件:" + file2.getAbsolutePath())
fileNum++
}
}
}
} else {
System.out.println("文件不存在!")
}
System.out.println("文件夹共有:" + folderNum + ",文件共笑仔桥有:"碰猛 + fileNum)
}
参考资料:百度百科 Java
Function SearchFiles(Path As String, FileType As String)Dim Files() As String '文件路径
Dim Folder() As String '文件夹路径
Dim a, b, c As Long
Dim sPath As String
sPath = Dir(Path &FileType) '查找第一个文件
Do While Len(sPath) '循环到没有文件为止
a = a + 1
ReDim Preserve Files(1 To a)
Files(a) = Path &sPath '将文件目录和文件名组合,并存放到数组中御枣
List1.AddItem Files(a) '加入list控件中
sPath = Dir '查找下一个文件
DoEvents '让出控制权
Loop
sPath = Dir(Path &"\镇弊拆", vbDirectory) '查找第一个文件夹
Do While Len(sPath) '循环到没有文件夹为止
If Left(sPath, 1) <>"." Then '为了防止重复查找
If GetAttr(Path &"\" &sPath) And vbDirectory Then '如果是文件夹则。。。。。。
b = b + 1
ReDim Preserve Folder(1 To b)
Folder(b) = Path &sPath &"\" '将目录和文件夹名称组合形成新的目录,并存放到数组中
End If
End If
sPath = Dir '查找下一个文件夹
DoEvents '让出控制权
Loop
For c = 1 To b '使用递归方法,遍历所有目录
SearchFiles Folder(c), FileType
Next
End Function
Private Sub Command1_Click() '调卜碧用
SearchFiles "e:\", "*.exe"
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)