Here you can find the source of stringToSize(String sizeString)
public static long stringToSize(String sizeString) throws ParseException
//package com.java2s; //License from project: Apache License import java.text.ParseException; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static long stringToSize(String sizeString) throws ParseException { Pattern pattern = Pattern.compile("(-?\\d+\\.?\\d*)([\\w]{0,2})", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(sizeString); if (matcher.matches()) { double baseSize = Double.parseDouble(matcher.group(1)); String unit = matcher.group(2).toLowerCase(); if (unit.equals("b") || unit.length() == 0) { return (long) baseSize; } else if (unit.equals("k") || unit.equals("kb")) { return (long) (baseSize * 1024); } else if (unit.equals("m") || unit.equals("mb")) { return (long) (baseSize * (1024 * 1024)); } else if (unit.equals("g") || unit.equals("gb")) { return (long) (baseSize * (1024 * 1024 * 1024)); } else if (unit.equals("e") || unit.equals("eb")) { return (long) (baseSize * (1024L * 1024 * 1024 * 1024)); }/*from w w w .j a va 2s. c om*/ } throw new ParseException(sizeString, 0); } }