File file = new File("c:");
String strs[] = file.list();
for(int i=0;i<strs.length;i++)
{
System.out.println(strs[i]);
}

解决方案 »

  1.   

    搂主是要知道目录的个数,又不是打印某些冬冬
    int count = 0;
    File dir = new File("c:");
    File[] files = dir.listFiles();
    for(int i = 0 ; i <files.length; i ++){
      if(files[i].isDirectory())
      count++;
    }
    return count;
      

  2.   

    昨天刚刚写了 统计传入的path下面的所有文件和目录 转成xml格式字符串
    /**
     * Created on 2005-3-18
     *  
     */
    package com.jeao.leon.util;import java.io.File;public class FileUtil {
        public static String file2XMLString(String path) {
            StringBuffer xmlString = new StringBuffer();
            File root = new File(path);
            if (root.isDirectory()) {
                xmlString.append("<Item type=\"folder\" path=\"").append(path)
                        .append("\">\n");
                String[] childFile = root.list();
                for (int i = 0; i < childFile.length; i++) {
                    File child = new File(path + "/" + childFile[i]);
                    if (child.isFile()) {
                        xmlString.append("<Item typ=\"file\" path=\"").append(path).append("/").append(
                                childFile[i]).append("\"/>\n");
                    } else if (child.isDirectory()) {
                        xmlString.append(file2XMLString(new StringBuffer(path)
                                .append("/").append(childFile[i]).toString()));
                    }
                }
                xmlString.append("</Item>\n");        } else {
                throw new IllegalArgumentException("paramerter is invalidate!");
            }
            return xmlString.toString();
        }
    }