Dealing with large files can be a challenge for beginners as well as for expert software developers. It is important to get the file size for optimizing the code performance, managing memory locations, manipulating the file contents, resource allocation, etc. In Java, the file size depends on the memory allocated to it for storage. There are multiple public classes for file handling such as File, FileInputStream, BufferedReader, etc.
This article provides distinct methods for determining the Java file size.
How to Get File Size in Java?
In Java, program execution is allocated memory space for storing declared variables or manipulating files, etc. Larger memory allocation will require more resources and comparatively the compilation time will also increase. To determine the file size in Java, you can use one of the following methods:
- Method 1: How to Get a File Size Using the File Class?
- Method 2: How to Get a File Size Using the File Channel Class?
- Method 3: How to Get a File Size Using FileInputStream Class?
- Method 4: How to Get a File Size Using the FileUtils Class?
Method 1: How to Get a File Size Using the File Class?
The Java File Class contains multiple methods that help us perform different file manipulation tasks, such as file creation, searching a file, modifying the file, etc. The object of the file class invokes different methods for handling the specified file. The following code gets the total file size using the File class in Java.
The following import statement includes the File class from the “java.io” package:
import java.io.File;
The fileSize object of the File class accepts the file’s path in the constructor. Replace the placeholder “<FilePath>” with your original file’s path. The “fileSize.length()” returns a numerical value that indicates the file’s size. Dividing the length by “1024” will give the file size in bytes as shown in the code below:
File fileSize = new File(<FilePath>);
System.out.println("File size returned by length() is = " + fileSize.length());
System.out.println("File size in bytes is = " + fileSize.length() /1024.0 + " bytes");
Complete Code & Output
Method 2: How to Get a File Size Using the FileChannel Class?
The Java FileChannel class is another public class for file handling. It is considered more efficient than the standard File class due to the multi-threading mechanism. The FileChannel class provides instances for reading from or writing to a file in Java.
The import statements include classes, packages, and necessary dependencies such as files and channels from the “java.nio” package, IOException from the java.io package:
import java.nio.file.*;
import java.io.IOException;
import java.nio.channels.*;
Inside the try block, the Path object “filePath” reads the file’s path using the get() method. Replace “<FilePath>” with the original file’s path. The “fileChannel” object from the FileChannel class opens the file by using the “filePath” object that initially stored the file’s path. The fileChannel.size() returns the size of the file in bytes which is then printed:
try
{
Path filePath = Paths.get("C:\\Users\\Desktop\\textdoc .txt");
FileChannel fileChannel = FileChannel.open(filePath);
System.out.println("File size " + fileChannel.size() + " bytes");
}
The catch block catches and displays any IOException that probably occurs by creating an instance “e” of this class:
catch(IOException e)
{
System.out.println(e);
}
Complete Code & Output
Method 3: How to Get a File Size Using the FileInputStream Class?
The Java FileInputStream class reads the file’s data in bytes from a file. It is extended from the InputStream class. The FileInputStream class provides methods for determining the file size in Java.
In this code snippet, the import statements are used to include FileInputStream and IOException class from the java.io package:
import java.io.FileInputStream;
import java.io.IOException;
Within the try block, the FileInputStream object “fileSize” accepts the file’s path in the constructor. Replace the “<FilePath>” with the original file’s path to determine its size. The “fileSize” object invokes the available() method that returns the size of the file in bytes:
try
{
FileInputStream fileSize = new FileInputStream("C:\\Users\\Desktop\\textdoc.txt");
System.out.println("Size of file is " + fileSize.available() + " bytes" ); }
The catch block is used for exception handling by creating an object of IOException class “e”:
catch(IOException e)
{
System.out.println(e);
}
Complete Code & Output
Method 4: How to Get a File Size Using the FileUtils Class?
The FileUtils class in Apache Common Lang is an advanced concept that is widely used for file handling. For this, the following dependency is to be added in the pom.xml file of Maven or build.gradle file of Gradle project:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.1</version>
</dependency>
Use the following import statements to include the File class from the java.io package and FileUtils class from the Apache Commons Lang as shown below:
import java.io.File;
import org.apache.commons.io.FileUtils;
The object “file” of the File class is created by providing the file’s path in the class’s constructor. The sizeOf() method of the FileUtils class returns the total bytes occupied by the file and the output is displayed:
File file = new File(<FilePath>);
System.out.println(FileUtils.sizeOf(file) + " bytes");
Complete Code & Output
That is all from this guide.
Conclusion
File handling is an important concept of Java in which multiple instances of different classes from various packages are used. In file handling, accessing, managing, and manipulating the data is effective and efficient. However, larger file sizes can impact the code performance and system IO resources. To get the Java file size, use the length() method of the File class, available() method of the FileInputStream class, or size() method of the FileUtils class. This article has provided and demonstrated multiple methods for getting the file size in Java.