首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

怎么列出某个目录下的所有文件

2012-03-13 
如何列出某个目录下的所有文件如何列出某个目录下的所有文件[解决办法].listFile()[解决办法]子目录下的文

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

[解决办法]
.listFile()
[解决办法]
子目录下的文件也要的话:

Java code
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) {        }    }} 

热点排行