package com.practice_25;
import java.io.File;
import java.util.Scanner;
/**
*
*
* 从键盘接收一个文件夹路径,统计该文件夹大小
*
*
*
* @author Janny
*
*/
public class day21Tongji {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String fileName = sc.nextLine();
File file = new File(fileName);
long spaceHold = getSpaceHold(file, 0);
System.out.println(spaceHold);
// File file = new File("aaa\\a.txt");
// System.out.println(file.isFile() + " " + file.length());
}
public static long getSpaceHold(File file, long s) {
// System.out
// .println(file.getName() + file.isFile() + " " + file.length());
if (file.isFile()) {
System.out.println(file.isFile() + file.getName() + " "
+ file.length());
return file.length();
} else if (file.isDirectory()) {
for (File file1 : file.listFiles()) {
// System.out.println(file1.isFile() + file1.getName() + " "
// + file1.length());
System.out.println(s);
s = getSpaceHold(file1, s) + s;
}
} else {
System.out.println("this is a wrong path");
return s;
}
return s;
}
}
|
|