Android 技术总结(014)—— 获取与转转文件的大小(B,KB,MB,GB)
/** * 取得文件大小 * * @param f * @return * @throws Exception */ public long getFileSizes(File f) throws Exception { long s = 0; if (f.exists()) { FileInputStream fis = null; fis = new FileInputStream(f); s = fis.available(); } else { f.createNewFile(); } return s; } /** * 转换文件大小 * * @param fileS * @return */ public String FormetFileSize(long fileS) { DecimalFormat df = new DecimalFormat("#.00"); String fileSizeString = ""; if (fileS < 1024) { fileSizeString = df.format((double) fileS) + "B"; } else if (fileS < 1048576) { fileSizeString = df.format((double) fileS / 1024) + "K"; } else if (fileS < 1073741824) { fileSizeString = df.format((double) fileS / 1048576) + "M"; } else { fileSizeString = df.format((double) fileS / 1073741824) + "G"; } return fileSizeString; }