Thursday 16 May 2013

How to get the Size of a File using Java Program?

Sailendra Narayan Jena

package com.lara.file;

import java.io.File;

public class FileSize
{
    public static void main(String[] args)
    {
File file =new File("c:\\abc.jpg");

if(file.exists())
{

double bytes = file.length();
double kilobytes = (bytes / 1024);
double megabytes = (kilobytes / 1024);

System.out.println("bytes : " + bytes);
System.out.println("kilobytes : " + kilobytes);
System.out.println("megabytes : " + megabytes);
}
else
{
System.out.println("File does not exists!");
}

    }
}

No comments:

Post a Comment