Showing posts with label file size in java. Show all posts
Showing posts with label file size in java. 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

Kali Linux Remote Desktop: Access GNOME from Windows Using Native RDP

  Kali Linux + GNOME 50 + GNOME Remote Desktop + Windows Remote Desktop (MSTSC) Getting a full GNOME desktop remotely on Kali Linux can be ...