分享

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

 秦志远 2010-03-21
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) {

        }
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多