How to get file size in Java?

The File.length() method get the file size in bytes.

Example: D:\\tmp\\nourit.txt  201Bytes

package com.nourit.javaIO;

import java.io.File;

public class GetFileSizeExample {

  public static void main(String[] args) {
    File file = new File("D:\\tmp\\nourit.txt");
    if(file.exists()){
      double bytes = file.length();
      System.out.println(bytes + " Bytes");
    }
  }

}

Output:

201.0 Bytes

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*