根据传入的文件大小,自动转换为B、KB、MB、GB、TB
import java.text.DecimalFormat;
public class FileSizeUtil{
/**
* 文件单位
*/
private static final String[] units = new String[]{"B","KB","MB","GB","TB"};
/**
* 文件大小自动转换
* @param size
* @return
*/
public static String fileSizeConvert(Long size){
if(size <= 0){
return "0";
}
Intger digitGroups = (int) (Math.log(size) / Math.log(1024));
digitGroups = digitGroups > 4 ? 4 : digitGroups;
return new DecimalFormat("#,##0.##")
.format(size / Math.pow(1024,digitGroups)) + " " + units[digitGroups];
}
}