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

© 403806022 中级黑马   /  2016-4-8 22:31  /  353 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class ZuoYe01 {
        public static void main(String[] args) throws FileNotFoundException, IOException {
                File file = getFile();
                ArrayList<Integer> list = new ArrayList<Integer>();
                int capacity = getNumOfFile(file,list);
                System.out.println(capacity);
        }
       
        public static File getFile(){
                Scanner scan = new Scanner(System.in);
                System.out.println("请您输入一个文件夹路径:");
                while(true){
                        String line = scan.nextLine();
                        File file = new File(line);
                        if(!file.exists()){
                                System.out.println("您输入的文件夹路径不存在,请您重新输入:");
                        }else if(file.isFile()){
                                System.out.println("您输入的是文件路径,请您重新输入:");
                        }else{
                                return file;
                        }
                }
        }
       
        public static int getNumOfFile(File dir,ArrayList<Integer> list) throws FileNotFoundException, IOException{
                File[] files = dir.listFiles();
                for (File file : files) {
                        if(file.isFile()){
                                list.add(new FileInputStream(file).available());
                               
                        } else if(file.isDirectory()){
                                getNumOfFile(file,list);
                        }
                }
                int sum =0;
                for (Integer integer : list) {
                        sum = sum + integer;
                }
                System.out.println(list.size());
                return sum;
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马