I have a file on a server and it is a zip file. How to check if the file size is bigger than 27 MB?
File file = new File("U:\intranet_root\intranet\R1112B2.zip");
if (file > 27) {
//do something
}
I have a file on a server and it is a zip file. How to check if the file size is bigger than 27 MB?
File file = new File("U:\intranet_root\intranet\R1112B2.zip");
if (file > 27) {
//do something
}
You can retrieve the length of the file with File#length(), which will return a value in bytes, so you need to divide this by 1024*1024 to get its value in mb.
Use the
length()
method of theFile
class to return the size of the file in bytes.You could combine the conversion into one step, but I've tried to fully illustrate the process.
Example :
Since Java 7 you can use
java.nio.file.Files.size(Path p)
.Kotlin Extension Solution
Add these somewhere, then call
if (myFile.sizeInMb > 27.0)
or whichever you need:If you'd like to make working with a String or Uri easier, try adding these:
If you'd like to easily display the values as a string, these are simple wrappers. Feel free to customize the default decimals displayed