package test;
import java.io.File;
/**
* <p>描述: 文件维护工具类
* 主要包括:获取文件夹容量大小
* </p>
* <p>创建时间: 2021/2/5 </p>
*
* @author hzz
* @version 1.0
*/
public class FileDelUtil {
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
File file = new File("D:\\output\\idea\\elasticsearch");
System.out.println("所在盘空间总大小:" + sizeFormat(file.getTotalSpace())); //所在盘空间总大小
System.out.println("所在盘空间可用大小:" + sizeFormat(file.getUsableSpace())); //所在盘空间总可用大小
System.out.println("所在盘空间总未占用大小:" + sizeFormat(file.getFreeSpace())); //所在盘空间总未占用大小
System.out.println("文件夹所用大小:" + sizeFormat(fileTotalSize(file)));
System.out.println("总耗时:" + timeFormat(System.currentTimeMillis() - start));
}
public static String timeFormat(long input){
return input / (3600 * 1000) +"小时" + ((input / 1000) % 3600) / 60 + "分钟" + (input / 1000) % 60 + "秒";
}
public static String sizeFormat(long input){
if (input < pow1024(1)){
return input + "B";
}else if (input >= pow1024(1) && input < pow1024(2)){
return input / pow1024(1) + "KB";
}else if (input >= pow1024(2) && input < pow1024(3)){
return input / pow1024(2) + "MB";
}else if (input >= pow1024(3) && input < pow1024(4)){
return input / pow1024(3) + "GB";
}else if (input >= pow1024(4) && input < pow1024(5)){
return input / pow1024(4) + "TB";
}else {
return input + "B";
}
}
static long total = 0;
static long cur = 0;
public static long fileTotalSize(File file){
long result = 0;
if (file != null){
total ++;
cur ++;
if (file.isDirectory()){
File[] files = file.listFiles();
total += files.length;
assert files != null;
for (File temp: files){
cur ++;
result += fileTotalSize(temp);
printSchedule(cur, total);
}
}else {
return file.length();
}
}
return result;
}
/**
* 百分比整型表示, 0-100
* @param cur
* @param total
* @return
*/
public static int intPercent(long cur, long total){
double result = (double) cur / (double) total * 100;
return new Double(Math.floor(result)).intValue();
}
public static long pow1024(int num){
long result = 1;
for (int i = 0; i < num; i ++){
result = result << 10;
}
return result;
}
/**
* 进度条总长度
*/
private static int TOTAL_LENGTH = 30;
public static void printSchedule(long cur, long total){
int percentInt = intPercent(cur, total);
for (int i = 0; i < TOTAL_LENGTH + 30; i++) {
System.out.print("\b");
}
//░▒
int now = TOTAL_LENGTH * percentInt / 100;
for (int i = 0; i < now; i++) {
System.out.print("▒");
}
for (int i = 0; i < TOTAL_LENGTH - now; i++) {
System.out.print(" ");
}
System.out.print(" " + percentInt + "%");
System.out.print(" " + cur + "/" + total);
}
}
07-20
5457