×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: joseifm
Added: Sep 23, 2014 12:26 PM
Views: 20
Tags: size file java
Return file size in B,KB.MB,GB,TB
  1. import java.text.DecimalFormat;
  2.  
  3. public  String FileSize(long size) {
  4.         if(size <= 0) return "0";
  5.         final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
  6.         int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
  7.         return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
  8.     }