My Android advanced tutorial ------) Java file size Conversion Tool class (size conversion between B, KB, MB, GB, TB, and PB)
Java file size Conversion Tool class (size conversion between B, KB, MB, GB, TB, and PB)
Sometimes you need to convert the file size and write the relevant code logic to demonstrate the effect of the file size as follows.
/***** Storage size (unit) converter. */public enum SizeConverter {/*** converts the size of any unit. The returned result contains two decimal places but not the unit. */Arbitrary {@ Override public String convert (float size) {while (size> 1024) {size/= 1024;} return String. format (FORMAT_F, size) ;}}, // bytes // unit/*** the conversion unit is the size of B. The returned result contains two decimal places and units. for example, 1024B-> 1KB, (1024*1024) B-> 1 MB */B {@ Override public String convert (float B) {return converter (0, b) ;}},/*** the conversion unit is KB. The returned result contains two decimal places and units. */KB {@ Override public String convert (float KB) {return converter (1, KB) ;},/*** the conversion unit is MB, the returned result contains two decimal places and units. */MB {@ Override public String convert (float MB) {return converter (2, MB) ;},/*** the conversion unit is GB, the returned result contains two decimal places and units. */GB {@ Override public String convert (float GB) {return converter (3, GB) ;},/*** the conversion unit is the size of TB, the returned result contains two decimal places and units. */TB {@ Override public String convert (float TB) {return converter (4, TB );}}, // ----------------------------------------------------------------------- // trim No unit/*** converts the size of any unit. When the return result decimal part is 0, two decimal places are removed, excluding units. */ArbitraryTrim {@ Override public String convert (float size) {while (size> 1024) {size/= 1024;} int sizeInt = (int) size; boolean isfloat = size-sizeInt> 0.0F; if (isfloat) {return String. format (FORMAT_F, size);} return String. format (FORMAT_D, sizeInt) ;}, // --------------------------------------------------------------------- // trim has a unit/*** the conversion unit is B, when the decimal part of the returned result is 0, two decimal places are removed and the Unit is included. */BTrim {@ Override public String convert (float B) {return trimConverter (0, B) ;},/*** the conversion unit is KB, when the decimal part of the returned result is 0, two decimal places are removed and the Unit is included. */KBTrim {@ Override public String convert (float KB) {return trimConverter (1, KB) ;},/*** the conversion unit is MB, when the decimal part of the returned result is 0, two decimal places are removed and the Unit is included. */MBTrim {@ Override public String convert (float MB) {return trimConverter (2, MB) ;},/*** the conversion unit is GB, when the decimal part of the returned result is 0, two decimal places are removed and the Unit is included. */GBTrim {@ Override public String convert (float GB) {return trimConverter (3, GB) ;},/*** the conversion unit is the size of TB, when the decimal part of the returned result is 0, two decimal places are removed and the Unit is included. */TBTrim {@ Override public String convert (float TB) {return trimConverter (4, TB );}};/****
Converts the specified size to a value in the range of 1024. Note that the maximum unit of this method is PB and the minimum unit is B. * Any unit beyond this range will be displayed **.
** @ Param size refers to the size to be converted. It must be a floating point number. Do not input it in integer mode, which may easily cause overflow. * (for example, 1024*1024*1024*1024 will overflow, so that the result is 0, because it first multiply the result by int and then convert it to float; * The 10240000f * 10240000f * 10240000f * 10240000f will not overflow) * @ return */abstract public String convert (float size ); // convert // unit conversion private static final String [] UNITS = new String [] {"B", "KB", "MB", "GB", "TB ", "PB ","**"}; Private static final int LAST_IDX = UNITS. length-1; private static final String FORMAT_F = "% 1 $-1.2f"; private static final String FORMAT_F_UNIT = "% 1 $-1.2f % 2 $ s "; private static final String FORMAT_D = "% 1 $-1d"; private static final String FORMAT_D_UNIT = "% 1 $-1d % 2 $ s "; // --------------------------------------------------------------------------- private static String converter (int unit, float siz E) {int unitIdx = unit; while (size> 1024) {unitIdx ++; size/= 1024;} int idx = unitIdx <LAST_IDX? UnitIdx: LAST_IDX; return String. format (FORMAT_F_UNIT, size, UNITS [idx]);} private static String trimConverter (int unit, float size) {int unitIdx = unit; while (size> 1024) {unitIdx +++; size/= 1024;} int sizeInt = (int) size; boolean isfloat = size-sizeInt> 0.0F; int idx = unitIdx <LAST_IDX? UnitIdx: LAST_IDX; if (isfloat) {return String. format (FORMAT_F_UNIT, size, UNITS [idx]);} return String. format (FORMAT_D_UNIT, sizeInt, UNITS [idx]);} // implements public static String convertBytes (float B, boolean trim) {return trim? TrimConvert (0, B, true): convert (0, B, true);} public static String convertKB (float KB, boolean trim) {return trim? TrimConvert (1, KB, true): convert (1, KB, true);} public static String convertMB (float MB, boolean trim) {return trim? TrimConvert (2, MB, true): convert (2, MB, true );}/****
Storage size unit conversion. Note that the maximum unit of this method is PB, and the minimum unit is B. * Any unit that exceeds this range is displayed **.
** @ Param unit start from which unit * @ param size refers to the storage size. Note that it is float. Do not input it in the form of an integer. Otherwise, it will overflow (for example, 1024*1024, * It first multiply 1024*1024 as an int and then convert it to float. If the value is too large, it will overflow. * So write 10241_f * 10241_f) * @ param withUnit determines whether the returned result String contains the corresponding unit * @ return */private static String convert (int unit, float size, boolean withUnit) {int unitIdx = unit; while (size> 1024) {unitIdx +++; size/= 1024 ;}if (withUnit) {int idx = unitIdx <LAST_IDX? UnitIdx: LAST_IDX; return String. format (FORMAT_F_UNIT, size, UNITS [idx]);} return String. format (FORMAT_F, size );}/****
Storage size unit conversion. If the decimal part is 0 after conversion, the decimal part is removed. * Note that the maximum unit of this method is PB and the minimum unit is B. Any unit beyond this range is displayed **.
** @ Param unit start from which unit * @ param size refers to the storage size. Note that it is float. Do not input it in the form of an integer. Otherwise, it will overflow (for example, 1024*1024, * It first multiply 1024*1024 as an int and then convert it to float. If the value is too large, it will overflow. * So write 10241_f * 10241_f) * @ param withUnit determines whether the returned result String contains the corresponding unit * @ return */private static String trimConvert (int unit, float size, boolean withUnit) {int unitIdx = unit; while (size> 1024) {unitIdx ++; size/= 1024 ;}int sizeInt = (int) size; boolean isfloat = size -SizeInt> 0.0F; if (withUnit) {int idx = unitIdx <LAST_IDX? UnitIdx: LAST_IDX; if (isfloat) {return String. format (FORMAT_F_UNIT, size, UNITS [idx]);} return String. format (FORMAT_D_UNIT, sizeInt, UNITS [idx]);} if (isfloat) {return String. format (FORMAT_F, size);} return String. format (FORMAT_D, sizeInt );}}
The following is a test class.
public class SizeConverterTest { public static void main(String[] args) { System.out.println(SizeConverter.BTrim.convert(6808055f)); System.out.println(SizeConverter.BTrim.convert(11475968f)); System.out.println(SizeConverter.BTrim.convert(5013547f)); System.out.println(SizeConverter.BTrim.convert(12623539f)); }}
Output result:
6.49Mb10.94Mb4.78Mb12.04Mb