java实现对文件大小进行格式后

//将文件进行
private String formatSize(long fileS) {
    DecimalFormat df = new DecimalFormat("#.00");
    String fileSizeString = "";
    String wrongSize = "0B";
    if (fileS == 0) {
        return wrongSize;
    }
    if (fileS < 1024) {
        fileSizeString = df.format((double) fileS) + "B";
    } else if (fileS < 1048576) {
        fileSizeString = df.format((double) fileS / 1024) + "KB";
    } else if (fileS < 1073741824) {
        fileSizeString = df.format((double) fileS / 1048576) + "MB";
    } else {
        fileSizeString = df.format((double) fileS / 1073741824) + "GB";
    }
    return fileSizeString;
}

 


需要的参数为文件的大小


System.out.println("文件大小是"+file.getSize());
String size =formatSize(file.getSize());//算出文件大小

你可能感兴趣的:(java,python,前端)