Java Get File Size
In this previous section of the Java IO, we have discussed various file operations such as write to file, read a file, rename a file, etc. In this section, we are going to discuss how to get the file size through a Java program.
There are the following ways to get the file size in Java.
Using Java IO
The Java IO package provides the classes that deal with file-related operations. In the same way, the Java IO package provides the File class that contains the length() method. That returns the size of the specified file in bytes. It returns an unspecified value if the file denotes a directory. The way can be used if you are using Java 7 or previous versions.
The Java File class is used to represent an abstract representation of file and directory pathnames. It belongs to the java.io package.
In the following program, we have created a constructor of the Files class and parsed the path of the file as a parameter. We have used the following methods of the class are:
exists(): The method checks whether the file or directory denoted by this abstract pathname exists or not. It returns true if and only if the specified path exists, false otherwise.
isFile(): The method checks whether the file denoted by this abstract pathname is a normal file. A file is normal if it is not a directory and, in addition, satisfies other system-dependent criteria. Any non-directory file created by a Java application is guaranteed to be a normal file. It returns true if and only if the file denoted by this abstract pathname exists and is a normal file, false otherwise.
length(): The method returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname represents a directory. The length, in bytes, of the file is denoted by this abstract pathname, or 0L if the file does not exist.
Let's see the Java program.
Using Java NIO Library
NIO is another Java IO library. It provides a different way of working with I/O in comparison to the standard IO library. It is an alternate IO API. The library is based on a buffer-oriented, channel-based approach for I/O operations.
The Java NIO library provides the class named Files. It belongs to java.nio.file package. The class provides static methods that operate on files, directories, or other types of files. In most cases, the methods defined in the class delegates to the associated file system provider to perform the file operations.
In order to get the file size, the Files class provides the size() method. The syntax of the size() method is:
The method accepts the path of the file as a parameter and returns the file size in bytes.
Java NIO library provides another class named FileChannel class. It is an abstract class that belongs to the java.nio.channels package. The class provides channels for reading, writing, mapping, and manipulating a file.
It provides the size() method to get the size of the file. It returns the current size of this channel's file, measured in bytes.
Let's see the Java program.
Using Apache Commons IO
Apache commons is a third-party library that provides general file manipulations utilities. The classes belong to the org.apache.commons.io package.
Using FileUtils.sizeOf() Method
The class provides the sizeOf() method to get the file size in bytes.
The method accepts a file or directory as a parameter. Note that the parameter must not be null. It returns the length (in bytes) of the file if the parameter is a regular file. If the argument is a directory then the size (bytes) of the directory is calculated recursively.
Note: It does not detect the overflow. So, we can get the negative file size due to overflow.
Before running the following program, ensure that the commons-io-2.4.jar file is added to the project.
The file size is: 65042816 bytes