Tuesday 22 November 2016

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


Click here to watch in Youtube :
https://www.youtube.com/watch?v=IBDU2a9YJn4&list=UUhwKlOVR041tngjerWxVccw

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:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_File_size_mb_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_File_size_mb_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/a3d05269db9922e504bb1f9de8d07031990c9910/BasicJava/JavaIODemo_File_size_mb_App/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment