I'd like to print file/directory sizes in Java, however for files larger than 1GB I cannot. For example, when I check a file with ~1.3 GB it writes 0.000 TB, this is shown on the debugging screenshot. The if-else jumps to the else branch. Probably the maximum limit of long type? Please advice me, the code is the following:
long size;
String notation;
if (file.isDirectory()) {
// File is a directory, calculate size of all files in the directory
size = 0;
for (File f : file.listFiles()) {
size += f.length();
}
} else {
// File is a regular file, use size of file
size = file.length();
}
// Conversion
long bytes = size;
long kilobytes = (bytes / 1024);
long megabytes = (kilobytes / 1024);
long gigabytes = (megabytes / 1024);
long terabytes = (gigabytes / 1024);
long petabytes = (terabytes / 1024);
long exabytes = (petabytes / 1024);
//long zettabytes = (exabytes / 1024);
//long yottabytes = (zettabytes / 1024);
if (size > 0 && size < kilobytes){
notation = "B";
System.out.println(size+notation);
} else if (size > kilobytes && size < megabytes) {
// file is smaller than 1 MB, return size in KB
size /= 1024;
notation = "KB";
System.out.println(size+notation);
} else if (size > megabytes && size < gigabytes) {
// file is smaller than 1 GB, return size in MB
notation = "MB";
size /= 1024.0 * 1024;
System.out.println(size+notation);
} else if (size > gigabytes && size < terabytes) {
// file is larger than 1 GB, return size in GB
notation = "GB";
size /= 1024.0 * 1024.0 * 1024.0;
System.out.println(size+notation);
} else {
// file is larger than 1 TB, return size in TB
notation = "TB";
size /= 1024.0 * 1024.0 * 1024.0 * 1024;
System.out.println(size+notation);
}
// cast size to double before passing it to String.format
return String.format("[Size: %.3f %s]", (double) size, notation);
}
Debugging shows the following:
EDIT:
I have changed to BigDecimal, the values seems correct, but still jumps to TB. Please see the screenshot:
SOLUTION: Now everything is perfect, thank you for all ;)
if (bsize.compareTo(BigDecimal.valueOf(1024)) < 0) {
notation = "B";
System.out.println(bsize+notation);
} else if (bsize.compareTo(BigDecimal.valueOf(1024*1024)) < 0) {
// file is smaller than 1 MB, return size in KB
bsize = bsize.divide(BigDecimal.valueOf(1024));
notation = "KB";
System.out.println(bsize+notation);
} else if (bsize.compareTo(BigDecimal.valueOf(1024L*1024L*1024L)) < 0) {
// file is smaller than 1 GB, return size in MB
notation = "MB";
bsize = bsize.divide(BigDecimal.valueOf(1024*1024));
System.out.println(bsize+notation);
} else if (bsize.compareTo(BigDecimal.valueOf(1024L*1024L*1024L*1024L)) < 0) {
// file is larger than 1 GB, return size in GB
notation = "GB";
System.out.println(bsize+notation);
bsize = bsize.divide(BigDecimal.valueOf(1024*1024*1024));
System.out.println(bsize+notation);
} else {
// file is larger than 1 TB, return size in TB
notation = "TB";
bsize = bsize.divide(BigDecimal.valueOf(1024L*1024L*1024L*1024L));
System.out.println(bsize+notation);
}