How to get File size in java

We can get File size in java by 2 methods.
1. API’s length() method
2. java.nio.file.Files API’s readAttributes() method.

In this post we will see examples on how to get file size in java by these 2 methods.

1. Example : File Size in Java using length()

length() method returns the size of the file in bytes. You can use this to compute File size in various formats.


If the File doesn’t exist in the given path, the the length method returns 0

2. Example : File Size in Java using readAttributes()



1. File Length method
1. File readAttributes method

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn