//获取文件夹路径
public static File getDir() {
Scanner sc = new Scanner(System.in);
System.out.println("input path:");
while (true) {
File dir = new File(sc.nextLine());
if (!dir.exists()) {
System.out.println("not exist!");
} else if (dir.isFile()) {
System.out.println("is file!");
} else {
return dir;
}
}
}
//获取文件大小
public static String getSize() {
String sum = null;
double k = (double)getSizeFun(getDir())/ 1024;
double m = k / 1024;
double g = m / 1024;
if (k >= 1 && k < 1024) {
sum = k + ".k";
} else if (m >= 1 && m < 1024) {
sum = m + ".m";
} else if (g >= 1 && g < 1024) {
sum = g + ".g";
}
String[] arr = sum.split("\\.");
return arr[0] + "." + arr[1].substring(0, 2) + arr[2].toUpperCase();
}
private static long getSizeFun(File dir) {
if (dir == null) {
System.out.println("null");
return 0;
}
File[] subFiles = dir.listFiles();
long sum = 0;
for (File subFile : subFiles) {
if (subFile.isFile()) {
sum += subFile.length();
} else {
sum += getSizeFun(subFile);
}
}
return sum;
}
//删除
public static void delete() {
deleteFun(getDir());
System.out.println("delete successful!");
}