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 ww.j ava2 s  .c o m*/
}

Result

Related Topic