Files size() method in Java with Examples
size() method of java.nio.file.Files help us to get the size of a file (in bytes). This method returns the file size, in bytes by taking the path of the file as a parameter. The size may differ from the actual size on the file system due to compression, support for sparse files, or other reasons. The size of files that are not regular files is implementation-specific and therefore unspecified.
public static long size(Path path) throws IOException
Parameters: This method accepts a parameter path which is the path to the file.
Return value: This method returns the file size, in bytes.
Exception: This method will throw following exceptions:
- IOException if an I/O error occurs.
- SecurityException in the case of the default provider, and a security manager is installed, its checkRead method denies read access to the file.
Below programs illustrate size?(Path) method:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.