阅读背景:

将文件的大小以最合适的单位显示

来源:互联网 
package com.lll.mgr;

import java.text.DecimalFormat;

/**
 * 工具类 。
 */
public class Util {

	/**
	 * 获取带单位的文件名,单位会自动显示为合适的值,如B、KB、MB等
	 * @param size 文件字节大小
	 */
	public static String readableFileSize(long size) {
		if(size <= 0) return "0";
	    final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
	    int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
	    return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
	}
	public static void main(String[] args) {
		System.out.println(Util.readableFileSize(1024));
	}
}

package com.lll.mgr;

import java.text.DecimalF



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: