Sunday, 11 June 2017

Java Tutorial : Java IO (Java File - How to get file size) ~ foundjava


Click here to watch in Youtube :

FileDemo.java
import java.io.File;

public class FileDemo
{

    public static void main(String[] args)
    {
        File file = new File("D:/work/java.txt");

        if (file.exists())
        {

            /*
             * Returns: The length, in bytes, of the file
             * denoted by this abstract pathname, or 0L if
             * the file does not exist.
             */
            double bytes = file.length();
            double kilobytes = (bytes / 1024);
            double megabytes = (kilobytes / 1024);
            double gigabytes = (megabytes / 1024);
            double terabytes = (gigabytes / 1024);
            double petabytes = (terabytes / 1024);
            double exabytes = (petabytes / 1024);
            double zettabytes = (exabytes / 1024);
            double yottabytes = (zettabytes / 1024);

            System.out.println("bytes : " + bytes);
            System.out.println("kilobytes : " + kilobytes);
            System.out.println("megabytes : " + megabytes);
            System.out.println("gigabytes : " + gigabytes);
            System.out.println("terabytes : " + terabytes);
            System.out.println("petabytes : " + petabytes);
            System.out.println("exabytes : " + exabytes);
            System.out.println("zettabytes : " + zettabytes);
            System.out.println("yottabytes : " + yottabytes);
        }
        else
        {
            System.out.println("File does not exists!");
        }
    }

}
Output
bytes : 2227920.0
kilobytes : 2175.703125
megabytes : 2.1247100830078125
gigabytes : 0.002074912190437317
terabytes : 2.0262814359739423E-6
petabytes : 1.978790464818303E-9
exabytes : 1.932412563299124E-12
zettabytes : 1.887121643846801E-15
yottabytes : 1.8428922303191414E-18
Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment