加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

java file 获取文件大小 返回 xx BT  xx KB xx MB

(2015-11-19 15:38:56)
标签:

javafile获取文件大小

返回xxbtxxkbxxmbxxgb

分类: java
   
   public static String GetFileSize(String Path){
    return GetFileSize(new File(Path));
   }
   
   
   public static String GetFileSize(File file){
    String size = ""; 
    if(file.exists() && file.isFile()){
    long fileS = file.length();
      DecimalFormat df = new DecimalFormat("#.00"); 
           if (fileS < 1024) {
               size = df.format((double) fileS) + "BT";
           } else if (fileS < 1048576) {
               size = df.format((double) fileS / 1024) + "KB";
           } else if (fileS < 1073741824) {
               size = df.format((double) fileS / 1048576) + "MB";
           } else {
               size = df.format((double) fileS / 1073741824) +"GB";
           }
    }else if(file.exists() && file.isDirectory()){
    size = "";
    }else{
    size = "0BT";
    }
    return size;
   }

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有