Saturday, March 12, 2016

Java I/O Examples : How To Get Size Of The File

Intro.

To get a size of the file, you can use the length() method in the java.io.File class. This method will return a long value.

Examples


package com.freesamplecode.java.io;

import java.io.File;

public class GetFileSizeDemo {
 public static void main(String[] args){
  File file = new File("D:/temp/testfile.txt");
  
  long fileSize = file.length();
  
  System.out.println("File size in bytes : "+fileSize);
  System.out.println("File size in KB : "+(double)fileSize/1024);
  System.out.println("File size in MB : "+(double)fileSize/(1024*1024));
 }
}


Output


File size in bytes : 44
File size in KB : 0.04296875
File size in MB : 4.1961669921875E-5

Screenshot

How To Get Size Of The File

Related Posts:

0 comments: