Here you can find the source of humanFileSize(Long longFileSize)
Parameter | Description |
---|---|
longFileSize | The size to convert |
public static String humanFileSize(Long longFileSize)
//package com.java2s; import java.text.DecimalFormat; import java.text.FieldPosition; public class Main { /**/*w w w . jav a 2s . com*/ * Converts the size of a file to a human-readable string, e.g. * "36 Kb", "1.2 Mb", etc. Contributor: Michael A. Russell * * @param longFileSize The size to convert * @return Human-readable string approximating that size. */ public static String humanFileSize(Long longFileSize) { /* If the input is negative, return a zero-length string. */ if (longFileSize < 0) return (""); /* If it's up to 512, use the number itself. */ if (longFileSize < 512) return (longFileSize.toString()); /* Provide a place to put the result of the division. */ double doubleBytes; /* We want at most two digits to the right of the decimal point. */ DecimalFormat outputFormat = new DecimalFormat("0.00"); /* Provide a place to put the converted value. */ StringBuffer outputStrBuf = new StringBuffer(); /* Provide a "FieldPosition" object. It looks like it's returned * by the "format( )" method, but I don't really care about it. * I haven't been able to find an example of how to use it, so * I'll just try using zero, and see what that does. */ FieldPosition fieldPos = new FieldPosition(0); /* If it's up to 1024 * 512, express in terms of kilobytes. */ if (longFileSize < (1024L * 512L)) { doubleBytes = longFileSize.doubleValue() / 1024.0; outputFormat.format(doubleBytes, outputStrBuf, fieldPos); return (outputStrBuf.toString() + " Kb"); } /* If it's up to 1024 * 1024 * 512, express in terms of megabytes. */ if (longFileSize < (1024L * 1024L * 512L)) { doubleBytes = longFileSize.doubleValue() / (1024.0 * 1024.0); outputFormat.format(doubleBytes, outputStrBuf, fieldPos); return (outputStrBuf.toString() + " Mb"); } /* If it's up to 1024 * 1024 * 1024 * 512, express in terms of * gigabytes. */ if (longFileSize < (1024L * 1024L * 1024L * 512L)) { doubleBytes = longFileSize.doubleValue() / (1024.0 * 1024.0 * 1024.0); outputFormat.format(doubleBytes, outputStrBuf, fieldPos); return (outputStrBuf.toString() + " Gb"); } /* If it's up to 1024 * 1024 * 1024 * 1024 * 512, express in terms of * terabytes. */ if (longFileSize < (1024L * 1024L * 1024L * 1024L * 512L)) { doubleBytes = longFileSize.doubleValue() / (1024.0 * 1024.0 * 1024.0 * 1024.0); outputFormat.format(doubleBytes, outputStrBuf, fieldPos); return (outputStrBuf.toString() + " Tb"); } /* If it's up to 1024 * 1024 * 1024 * 1024 * 1024 * 512, express in * terms of petabytes. */ if (longFileSize < (1024L * 1024L * 1024L * 1024L * 1024L * 512L)) { doubleBytes = longFileSize.doubleValue() / (1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0); outputFormat.format(doubleBytes, outputStrBuf, fieldPos); return (outputStrBuf.toString() + " Pb"); } /* Express in exabytes. A long integer can be at most 2**63 - 1, * and that's about 9 exabytes, so we don't need to go higher. */ doubleBytes = longFileSize.doubleValue() / (1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0); outputFormat.format(doubleBytes, outputStrBuf, fieldPos); return (outputStrBuf.toString() + " Eb"); } }