Java Program to Get the File Size and File Path

You are currently viewing Java Program to Get the File Size and File Path

Java program to get the file size and file path

// Java program to get file size 
// and file path

import java.io.File;

public class GetFilePathAndSize {
  public static void main(String args[]) {
    final String fileName = "file1.txt";
    try {
      //File Object
      File objFile = new File(fileName);

      //getting file path
      System.out.println("File path is: " + objFile.getAbsolutePath());
      //getting filesize
      System.out.println("File size is: " + objFile.length() + " bytes.");
    } catch (Exception Ex) {
      System.out.println("Exception: " + Ex.toString());
    }

  }
}

Output

File path is: /app/file1.txt
File size is: 0 bytes.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply