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;
}
}
|
|