How to Get File Information in Java


In this example we will show the method to get file information in Java.

Source Code

package com.beginner.examples;

import java.io.File; 

public class GetFileInfo {  
  public static void main(String[] args) {

    File file = new File("example.txt");
    if(!file.exists()) {
        System.out.println("File does not exist.");
        return;
    }
    System.out.println("Name: " + file.getName()); // name
    System.out.println("Absolute Path: " + file.getAbsolutePath()); // Absolute Path
    System.out.println("File size: " + file.length()); // File size
    System.out.println("Readable: " + file.canRead()); // Readable
    System.out.println("Writeable: " + file.canWrite()); // Writeable
  }
}

Output:

Name: example.txt
Absolute Path: D:Workspacescasesexample.txt
File size: 10
Readable: true
Writeable: true

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments