WebHosting

Showing posts with label java file size. Show all posts
Showing posts with label java file size. Show all posts

Thursday, April 12, 2012

Read file size in java/ Java get file size


import java.io.File;

public class FileUtilityity {


 /**
     * Function will return the file size
     * @param filename : file name with full path
     * @return size fo the file in bytes
     */

    public long readFileSize(String filename)
{
        File file = new File(filename);
        if (!file.exists() || !file.isFile()) {
            System.out.println("File doesn\'t exist");
            return -1;
     
 }
        //Will return the file size
        return file.length();
   
}

public static void main(String[] args)
{
        FileUtility FileUtility = new FileUtility();
        long size = FileUtility.readFileSize("/temp/myfile1.txt");
        System.out.println("File Size in bytes: " + size);
        System.out.println("File Size in Kb: " + size / 1024);
        System.out.println("File Size in Mb: " + (size / 1024) / 1024);
    }
}

Featured Posts

How to Enable and Configure Remote Desktop (RDP) on Ubuntu 24.04.4 LTS

Remote Desktop Protocol (RDP) support in Ubuntu has become considerably easier with the GNOME Remote Desktop stack included in modern Ubuntu...