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



   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;
   }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM