// print sizes in human readable format (e.g., 1K 23M 4G)
public static String fileSizeToString(File file) {
String size;
DecimalFormat df = new DecimalFormat("#.##");
if (file.exists()) {
double bytes = file.length();
/*
double kilobytes = (bytes / 1024);
double megabytes = (kilobytes / 1024);
double gigabytes = (megabytes / 1024);
double terabytes = (gigabytes / 1024);
double petabytes = (terabytes / 1024);
double exabytes = (petabytes / 1024);
double zettabytes = (exabytes / 1024);
double yottabytes = (zettabytes / 1024);
*/
if (bytes < 1024) {
size = df.format(bytes) + "B";
} else if (bytes < 1024 * 1024) {
size = df.format(bytes / 1024) + "K";
} else if (bytes < 1024 * 1024 * 1024) {
size = df.format(bytes / 1024 / 1024) + "M";
} else if (bytes < 1024 * 1024 * 1024 * 1024) {
size = df.format(bytes / 1024 / 1024 / 1024) + "G";
} else if (bytes < 1024 * 1024 * 1024 * 1024 * 1024) {
size = df.format(bytes / 1024 / 1024 / 1024 / 1024) + "T";
} else if (bytes < 1024 * 1024 * 1024 * 1024 * 1024 * 1024) {
size = df.format(bytes / 1024 / 1024 / 1024 / 1024 / 1024) + "P";
} else {
size = "huge";
}
} else {
size = "error";
Log.e(TAG, file.getName() + " can not be found");
}
return size;
}
网友评论