Q:

Java program to get file size and file path

belongs to collection: Java File Handling Programs

0

This program will print the exact (absolute) path of the file and file length i.e. file size in bytes, to get the file path we will use the File.getAbsolutePath() method and to get file length (size) we will use File.length() Method.

File.getAbsolutePath() - Method return absolute path of the file, this is the method of File class.

File.length() - Method returns the file size (length) in the bytes, this is also method of File class.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Get File Length (Size) and File Path in Java

// 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: d:/programs/file1.txt
File size is: 40 bytes.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java File Handling Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to delete a file... >>
<< Java program to read content from file using Buffe...