Java - File Input Output File Size

Introduction

You can get the size of a file in bytes using the length() method of the File class.

File myFile = new File("myfile.txt");
long fileLength = myFile.length();

If the actual File does not exist, the length() method returns zero.

If it is a directory name, the return value is not specified.

The return type of the length() method is long, not int.

Demo

import java.io.File;

public class Main {
  public static void main(String[] args) {
    File myFile = new File("Main.java");
    long fileLength = myFile.length();
    System.out.println(fileLength);
  }/* w  w  w  .ja va 2s .co m*/
}

Result

Related Topic