如何用java返回一个文件夹的所有文件(含有子文件夹)的名字

如何用java返回一个文件夹的所有文件(含有子文件夹)的名字,第1张

/** 把铅州下边的直接复制到你的类中

* 通过目录遍历文件

*/

public static void fileTest(File file, String temp)

{

//是文件的情况

if (file.isFile())

{

System.out.print("文件 :" + file.getName() + "\t")

}//是目录的情况

else

{

File[] files = file.listFiles()

for (File fileTemp : files)

{

if (fileTemp.isDirectory())

{

System.out.println(temp + "目录 :" + fileTemp.getName() + "\t")

fileTest(fileTemp, temp + "\t")

}

else

{

System.out.println(temp + "文件 :" + fileTemp.getName() + "\t")

}

}

}

}

public static void main(String[] args)

{

/槐宴蔽/newFile中输入文件夹路径 new File(path)

File file = new File(System.getProperty("user.dir") + "/inoroutput/file")

fileTest(file, ""祥茄)

}

控件:按钮 列表框 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 + 1ReDim Preserve Files(1 To a) Files(a) = Path &sPath '文件目录和文件名组合,并存放数组 List1.AddItem Files(a) '加入list控件 sPath = Dir '查找下文件 DoEvents '让出控制权LoopsPath = Dir(Path &"\", vbDirectory) '查找第文件夹 Do While Len(sPath) '循环没有文件夹止 If Left(sPath, 1) <"." Then '了防止重复查找 If GetAttr(Path &"\" &sPath) And vbDirectory Then '文件夹则 b = b + 1ReDim Preserve Folder(1 To b) Folder(b) = Path &sPath &"\" '目录和文件夹名称组合形成新目录,并存放数组凯答End IfEnd IfsPath = Dir '查找下文件夹 DoEvents '让出控伍散制权LoopFor c = 1 To b '使用递归方法遍历腔孙氏所有目录 SearchFiles Folder(c), FileTypeNextEnd Function


欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/tougao/8171811.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存