Java基础知识—File文件操作练习作业—02
2、键盘录入一个文件夹路径,统计该文件夹的大小
import java.io.File;
import java.util.Scanner;
/**
*
* @author Fight_steven
*/
public class CountFileContains {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
while(true){
System.out.println("please input file path:");
String path = in.nextLine();
File filepath = new File(path);
if(!filepath.exists()){
System.out.println("Error! input file path try once again:");
continue;
}
countContains(path);
break;
}
}
static int sum;
public static void countContains(String path){
String tp = path;
File filepath = new File(path);
String[] paths = filepath.list();
for(int x = 0;x < paths.length;x++){
path = tp + "\\" + paths[x];
File tmp = new File(path);
if(tmp.isFile()){
sum += tmp.length();
System.out.println(tmp);
System.out.println(sum);
path = "";
}else if(tmp.isDirectory()){
countContains(path);
}
}
}
}