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