当前位置: 代码迷 >> Android >> Android 技术小结(014)—— 获取与转转文件的大小(B,KB,MB,GB)
  详细解决方案

Android 技术小结(014)—— 获取与转转文件的大小(B,KB,MB,GB)

热度:152   发布时间:2016-05-01 14:59:51.0
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;    }


  相关解决方案