🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Determining the size of a file is a common task in many applications. In Java, this can be accomplished using the File class from the java.io package or the Files class from the java.nio.file package. This blog post will guide you through the process of obtaining the file size and converting it into various units such as bytes, kilobytes (KB), megabytes (MB), gigabytes (GB), and terabytes (TB).
Table of Contents
- Introduction
- Using the FileClass
- Using the FilesClass from NIO
- Conversion Methods
- Example: Displaying File Sizes in Different Units
- Conclusion
Introduction
Java provides multiple ways to determine the size of a file. The File class offers a simple method to get the file size in bytes. For more advanced file operations, the Files class from the java.nio.file package can be used. Once the file size in bytes is obtained, it can be converted to other units such as KB, MB, GB, and TB using simple arithmetic.
Using the File Class
The File class provides the length method to get the size of a file in bytes.
Example
import java.io.File;
public class FileSizeUsingFileClass {
    public static void main(String[] args) {
        File file = new File("example.txt");
        long fileSizeInBytes = file.length();
        System.out.println("File size in bytes: " + fileSizeInBytes);
    }
}
Explanation:
- A Fileobject is created with the file name.
- The lengthmethod is called to get the file size in bytes.
Using the Files Class from NIO
The Files class provides a more modern and flexible approach to obtaining the file size.
Example
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
public class FileSizeUsingNIO {
    public static void main(String[] args) {
        Path filePath = Paths.get("example.txt");
        try {
            long fileSizeInBytes = Files.size(filePath);
            System.out.println("File size in bytes: " + fileSizeInBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Explanation:
- A Pathobject is created using thePaths.getmethod with the file name.
- The Files.sizemethod is called to get the file size in bytes.
- Exceptions are handled using a try-catchblock to catch anyIOExceptionthat may occur during the file size retrieval process.
Conversion Methods
To convert the file size from bytes to other units, simple arithmetic is used. The following conversion factors are used:
- 1 KB = 1024 bytes
- 1 MB = 1024 KB
- 1 GB = 1024 MB
- 1 TB = 1024 GB
Example
public class FileSizeConverter {
    public static void main(String[] args) {
        long fileSizeInBytes = 10485760; // Example file size in bytes
        double fileSizeInKB = (double) fileSizeInBytes / 1024;
        double fileSizeInMB = (double) fileSizeInKB / 1024;
        double fileSizeInGB = (double) fileSizeInMB / 1024;
        double fileSizeInTB = (double) fileSizeInGB / 1024;
        System.out.println("File size in bytes: " + fileSizeInBytes);
        System.out.println("File size in KB: " + fileSizeInKB);
        System.out.println("File size in MB: " + fileSizeInMB);
        System.out.println("File size in GB: " + fileSizeInGB);
        System.out.println("File size in TB: " + fileSizeInTB);
    }
}
Explanation:
- The file size in bytes is divided by the conversion factors to get the file size in KB, MB, GB, and TB.
- The results are printed to the console.
Example: Displaying File Sizes in Different Units
Combining all the methods, we can create a comprehensive example that reads the file size and displays it in various units.
Example
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
public class FileSizeExample {
    public static void main(String[] args) {
        String fileName = "example.txt";
        // Using File class
        File file = new File(fileName);
        long fileSizeInBytes = file.length();
        printFileSize(fileSizeInBytes);
        // Using Files class
        Path filePath = Paths.get(fileName);
        try {
            fileSizeInBytes = Files.size(filePath);
            printFileSize(fileSizeInBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void printFileSize(long fileSizeInBytes) {
        double fileSizeInKB = (double) fileSizeInBytes / 1024;
        double fileSizeInMB = fileSizeInKB / 1024;
        double fileSizeInGB = fileSizeInMB / 1024;
        double fileSizeInTB = fileSizeInGB / 1024;
        System.out.println("File size in bytes: " + fileSizeInBytes);
        System.out.println("File size in KB: " + fileSizeInKB);
        System.out.println("File size in MB: " + fileSizeInMB);
        System.out.println("File size in GB: " + fileSizeInGB);
        System.out.println("File size in TB: " + fileSizeInTB);
    }
}
Explanation:
- The file size is obtained using both the Fileclass and theFilesclass.
- The printFileSizemethod converts the file size from bytes to KB, MB, GB, and TB and prints the results.
Conclusion
Getting the file size in Java can be accomplished using the File class or the Files class from the NIO package. Once the file size in bytes is obtained, it can be converted to other units such as KB, MB, GB, and TB using simple arithmetic. By understanding these methods, you can efficiently determine and display file sizes in your Java applications.
Feel free to experiment with the code examples provided in this tutorial to gain a deeper understanding of how to get file sizes in Java. Happy coding!
 
 
 
![[NEW] Full-Stack Java Development with Spring Boot 3 & React Build 5 Spring Boot Projects with Java: Line-by-Line Coding](https://img-c.udemycdn.com/course/750x422/5338984_4d3a_5.jpg) 
 
 
 
 
 
 
 
 
 
 
Comments
Post a Comment
Leave Comment