如何列出某个目录下的所有文件

FEIFEI12345678 2008-07-07 01:25:26
如何列出某个目录下的所有文件
...全文
719 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
aheroboy 2010-03-21
  • 打赏
  • 举报
回复
public static void getAllDirectoryAndFile(String strPath){
try {
File[] fList = new File(strPath).listFiles();
if ((fList == null) || (fList.length <= 0)) {
System.out.println("空文件夹");
return;
}else{
for(File s:fList){
System.out.println(s.getPath());
getAllDirectoryAndFile(s.getPath());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Jidom 2009-11-29
  • 打赏
  • 举报
回复

String[] fs=new File(localRoot).list();
if ((fs == null) || (fs.length <= 0)) {
System.out.println("空文件夹");
return;
}
for(String s:fs){
System.out.println(s);
}
Jidom 2009-11-29
  • 打赏
  • 举报
回复
String[] fs=new File(localRoot).list();
if ((fs == null) || (fs.length <= 0)) {
System.out.println("空文件夹");
return;
}
for(String s:fs){
System.out.println(s);
}
lord_is_layuping 2008-07-07
  • 打赏
  • 举报
回复
子目录下的文件也要的话:

import java.io.*;

class Test1 {
static void getDir(String strPath) throws Exception {
try {
File f = new File(strPath);
if (f.isDirectory()) {
File[] fList = f.listFiles();
for (int j = 0; j < fList.length; j++) {
if (fList[j].isDirectory()) {
System.out.println(fList[j].getPath());
getDir(fList[j].getPath()); // 在getDir函数里面又调用了getDir函数本身
}
}
for (int j = 0; j < fList.length; j++) {

if (fList[j].isFile()) {
System.out.println(fList[j].getPath());
}

}
}
} catch (Exception e) {
System.out.println("Error: " + e);
}

}

public static void main(String[] args) {
String strPath = "C:\\Documents and Settings\\All Users\\Documents\\My Music";

System.out.println(strPath);

try {
getDir(strPath);
} catch (Exception e) {

}
}
}
zhaolinger2 2008-07-07
  • 打赏
  • 举报
回复
.listFile()

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧