Since Java 7 you can use java.nio.file.Files.size(Path p)
.
Path path = Paths.get("C:\\1.txt");
long expectedSizeInMB = 27;
long expectedSizeInBytes = 1024 * 1024 * expectedSizeInMB;
long sizeInBytes = -1;
try {
sizeInBytes = Files.size(path);
} catch (IOException e) {
System.err.println("Cannot get the size - " + e);
return;
}
if (sizeInBytes > expectedSizeInBytes) {
System.out.println("Bigger than " + expectedSizeInMB + " MB");
} else {
System.out.println("Not bigger than " + expectedSizeInMB + " MB");
}