The tutorial below will show any Java user how they can read the size of a file in bytes and convert it into the most appropriate format (i.e. kilobytes and megabytes) in order to display the size to the user in a simple format.

This tutorial shows how to open a file based on the value that you give the filenameString variable, read its size in bytes and if necessary, convert the value to a kilobytes or megabytes value and finally display the value to the user. The code to do so is highlighted below and comes in two forms: commented and uncommented as to improve readability:

Commented code:

package getfilesize;

/*
* Title:       GetFileSize
* Version:     1.0.0
* Date:        20/01/2012
* Description: Displays the size of a file, who’s path is defined by the value of filenameString.
* Author:      Dillon Chaffey
* Website:     https://chafflube.wordpress.com
* E-mail:      chafflube@hotmail.com
*/

import java.io.*;

public class GetFileSize {

    // Create some static variables to be used within the main method below
    static String filenameString;
    static File file;
    static long fileSizeBytesLong;
   
    public static void main(String[] args) {
        // Assign a value to the the filenameString variable below. This value must be the absolute path to the file the user wishes to open
        filenameString = "<path to file goes here>";
       
        try {
            // Try to open a new file using the value of the filenameString variable as the file path           
            file = new File(filenameString);
        }
        // If something goes wrong when trying to open the file display some information to the user about what went wrong
        catch (Exception e) {
            e.printStackTrace();
        }
       
        // Make the value of the fileSizeBytesLong variable equal to the amount of bytes within the opened file using file.length()
        fileSizeBytesLong = file.length();
       
        // Check whether the file is less than 1024 bytes and inform the user of the size in bytes, else inform the user of the size in KB, or MB
        if (file.length() < 1024) {
            System.out.println("The size of the file at: \"" + filenameString + "\" is: " + fileSizeBytesLong + "bytes.");
        }
        if (file.length() >= 1024 && file.length() < 1048576) {
            System.out.println("The size of the file at: \"" + filenameString + "\" is: " + (fileSizeBytesLong / 1024) + "kilobytes.");
        }
        if (file.length() >= 1048576 && file.length() < 1073741824) {
            System.out.println("The size of the file at: \"" + filenameString + "\" is: " + ((fileSizeBytesLong / 1024) / 1024) + "megabytes.");
        }

        // Close the program
        System.exit(0);
    }
}

Uncommented code:

package getfilesize;

/*
* Title:       GetFileSize
* Version:     1.0.0
* Date:        20/01/2012
* Description: Displays the size of a file, who’s path is defined by the value of filenameString.
* Author:      Dillon Chaffey
* Website:     https://chafflube.wordpress.com
* E-mail:      chafflube@hotmail.com
*/

import java.io.*;

public class GetFileSize {

    static String filenameString;
    static File file;
    static long fileSizeBytesLong;
   
    public static void main(String[] args) {
        filenameString = "<path to file goes here>";
       
        try {
            file = new File(filenameString);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
       
        fileSizeBytesLong = file.length();
       
        if (file.length() < 1024) {
            System.out.println("The size of the file at: \"" + filenameString + "\" is: " + fileSizeBytesLong + "bytes.");
        }
        if (file.length() >= 1024 && file.length() < 1048576) {
            System.out.println("The size of the file at: \"" + filenameString + "\" is: " + (fileSizeBytesLong / 1024) + "kilobytes.");
        }
        if (file.length() >= 1048576 && file.length() < 1073741824) {
            System.out.println("The size of the file at: \"" + filenameString + "\" is: " + ((fileSizeBytesLong / 1024) / 1024) + "megabytes.");
        }

        System.exit(0);
    }
}

Also, note that the above code will not execute without errors until the value of the filenameString has been edited to reflect the absolute file path of an existing file.

Hopefully this tutorial has allowed you to gain a better understanding of the workings of the Java programming language. Good luck with your current and future Java projects.

Advertisements