You can use the length() method on File which returns the size in bytes.
Use the length() method of the File class to return the size of the file in bytes.
// Get file from file name
File file = new File("U:\intranet_root\intranet\R1112B2.zip");
// Get length of file in bytes
long fileSizeInBytes = file.length();
// Convert the bytes to Kilobytes (1 KB = 1024 Bytes)
long fileSizeInKB = fileSizeInBytes / 1024;
// Convert the KB to MegaBytes (1 MB = 1024 KBytes)
long fileSizeInMB = fileSizeInKB / 1024;
if (fileSizeInMB > 27) {
...
}
You could combine the conversion into one step, but I've tried to fully illustrate the process.
Try following code:
File file = new File("infilename");
// Get the number of bytes in the file
long sizeInBytes = file.length();
//transform in MB
long sizeInMb = sizeInBytes / (1024 * 1024);
Logic
You can simplify your logic by testing for the biggest known extension, for example
if (fileSize > 1024 * 1024)
doMiB();
} else if (fileSize > 1024) {
doKiB();
} else {
doByte();
}
Rounding
Don't use Math.round(x / 100.0) * 100. for rounding to two decimal places use the appropriate library function
Argument checking
Don't use default values ("Unknown") for invalid input. If the input to your getFileSize method is not a file, throw an exception
if (!file.isFile()) {
throw new IllegalArgumentException("Expected argument to be a file");
}
Fast return
This is more personal taste but I prefer to return as soon as possible. If you know the result will be "100.31 KiB" why save it into a local variable instead of returning it immediately?
Conclusion
I would improve the code as followed
private static final DecimalFormat format = new DecimalFormat("#.##");
private static final long MiB = 1024 * 1024;
private static final long KiB = 1024;
public String getFileSize(File file) {
if (!file.isFile()) {
throw new IllegalArgumentException("Expected a file");
}
final double length = file.length();
if (length > MiB) {
return format.format(length / MiB) + " MiB";
}
if (length > KiB) {
return format.format(length / KiB) + " KiB";
}
return format.format(length) + " B";
}
Use helper functions
The logic performed by your math isn't immediately obvious at a glance. You should introduce a helper function:
static double roundToDecimals(double aValue, int aDecimals){
...
}
Even better, just use DecimalFormat:
DecimalFormat df = new DecimalFormat("#.##");
String output = df.format(1.2345678); // "1.23"
Work in logarithms
You can simplify your determination of the correct suffix by using the logarithm of the file size.
First of note that \$1024 = 2^{10}\$, \$1024*1024 = 2^{20}\$ and so forth. You're checking if \$x < 2^{10}\$ and then if \$x < 2^{20}\$ etc.
It's much easier to take the two-logarithm of \$x\$ and then check \$\log_2(x) < 10\$, \$\log_2(x) < 20\$ etc...
Now if you take \$k=floor(\log_2(x) / 10)\$ you will end up with \$k=0\$ for bytes, \$k=1\$ for KiB, \$k=2\$ for MiB etc. Which means that you can simply index into an array of the suffixes to get the right suffix, then upscale the result to get the correct number of digits.
Like this:
public static double log2(long n){
// Implement this but without inaccuracies due to FP math.
// Just count the number of leading zeros and do the math.
return (Math.log(n) / Math.log(2));
}
public static String getFileSize(File file) {
...
long logSize = (long)log2(fileSize);
final String[] suffixes = new String[]{" B", " KiB", " MiB", " GiB", " TiB", " PiB", " EiB", " ZiB", " YiB"};
int suffixIndex = (int) (logSize / 10); // 2^10 = 1024
double displaySize = fileSize / Math.pow(2, suffixIndex*10);
DecimalFormat df = new DecimalFormat("#.##");
return df.format(displaySize) + suffixes[suffixIndex];
Other comments
You should keep your fileSize in long format for accuracy.
Also you can move the fileSize variable into the scope of the if where it is initialized and used.
Try:
File file =new File("myfile_in_test.java");
if(file.exists()){
final double bytes = file.length();
final double kilobytes = (bytes / 1024);
System.out.println("bytes : " + bytes);
System.out.println("kilobytes : " + kilobytes);
}else{
System.out.println("File does not exists!");
}
Here is an example which shows that it works just fine.
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
File file = File.createTempFile("deleteme", ".txt");
file.deleteOnExit();
Thread main = Thread.currentThread();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
do {
Thread.sleep(200);
long length = file.length();
System.out.println("File " + file + " is " + length + " bytes long.");
} while(main.isAlive());
System.out.println("Finished");
} catch (InterruptedException e) {
System.err.println("Interrupted");
}
}
}, "monitor");
t.start();
FileOutputStream fos = new FileOutputStream(file);
for(int i=0;i<2000;i++) {
fos.write("words words words words words words words words words words words\n".getBytes());
Thread.sleep(1);
}
fos.close();
}
}
prints something like.
File /tmp/deleteme4214599935706768614.txt is 11880 bytes long.
File /tmp/deleteme4214599935706768614.txt is 23562 bytes long.
File /tmp/deleteme4214599935706768614.txt is 35376 bytes long.
File /tmp/deleteme4214599935706768614.txt is 47256 bytes long.
File /tmp/deleteme4214599935706768614.txt is 59136 bytes long.
File /tmp/deleteme4214599935706768614.txt is 70950 bytes long.
File /tmp/deleteme4214599935706768614.txt is 82830 bytes long.
File /tmp/deleteme4214599935706768614.txt is 94644 bytes long.
File /tmp/deleteme4214599935706768614.txt is 106524 bytes long.
File /tmp/deleteme4214599935706768614.txt is 118338 bytes long.
File /tmp/deleteme4214599935706768614.txt is 130218 bytes long.
File /tmp/deleteme4214599935706768614.txt is 132000 bytes long.
Finished