Java Get File Information Example

Java Get File Information Example


Get File Information In Java
To get the file information which is already created, or already exists. 
If it exists we can use many methods like getName(), getPath(), etc.
Now we will understand this with the help of an example -
Example - 
//online promotion house
// web designing house
 
import second.*;

import java.io.File;
import java.io.IOException;
import java.util.*;
// Post decrement operator
class Main{


  public static void main(String[] args) {

  try{
      File f = new File("FirstFile.txt");
if (f.exists()){
          System.out.println("Name: "+ f.getName());
          System.out.println("Path: "+ f.getAbsolutePath());
          System.out.println("Size: "+ f.length());
          System.out.println("Writeable: "+ f.canWrite());
          System.out.println("Readable: "+ f.canRead());
      }
 
else
{
          System.out.println("The File does not exist");
  }
  }
  catch (Exception exception){
System.out.println("Error Occured = "+exception);
  }
      }
  }

 
Output -