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);
    }
}

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

#Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc

 #Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc Linux is an open-source operating system that is loved by millio...