A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© a290741206 中级黑马   /  2016-9-17 12:47  /  401 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/**
*
*/
package com.heima.test2;

import java.io.File;
import java.util.HashMap;

public class Test1 {

        /**
         * 1.        统计D盘下的所有文件的扩展名分别是什么,共有多少种类型的文件(扩展名);
         * 并将每种扩展名以及个数打印到控制台上

         */
        public static void main(String[] args) {
                File dir = new File("D:\\");
                print(dir);
        }

        /**
         * @param file
         */
        private static void print(File dir) {
                File[] file =dir.listFiles();
                if (file==null) {
                        return;
                }
                HashMap<String, Integer> hm = new HashMap<>();
                for (File file2 : file) {
                        if (file2.isFile()) {
                        String[] str=file2.getName().split(".");
                        if (!hm.containsKey(str[1])) {
                                hm.put(str[1], 1);
                        } else {
                                hm.put(str[1], 1+hm.get(str[1]));
                        }
                        } else {
                                print(file2);
                        }
                }
                for (String key : hm.keySet()) {
                        Integer value = hm.get(key);
                        System.out.println("扩展名有" +key+ "共有" + value);
                }
        }
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
        at com.heima.test2.Test1.print(Test1.java:33)
        at com.heima.test2.Test1.print(Test1.java:39)
        at com.heima.test2.Test1.print(Test1.java:39)
        at com.heima.test2.Test1.main(Test1.java:18)

3 个回复

倒序浏览
活雷锋在哪里
回复 使用道具 举报
自己顶一下可以不
回复 使用道具 举报
怎么问题才会有人回..........
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马