Displaying File Properties

Aim: 
To write a java program that reads a file name from the user then displays information about whether the file exists, whether the file is readable, whether the file is writable, type of file and the length of the file in bytes.

Description:
A File object is used to get information or manipulate the information associated with a disk file; such as the permissions, time, date, directory path, is it readable or writable, file size, and so on. File class defines many methods that obtain the standard properties of a File object. For example, getName () returns the file name, and exists() returns true if the file exists, false if it does not.

Program:
import java.io.*;
class FileProp
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader (
System.in));
System.out.println("Enter file name :");
String fname = br.readLine();
File f = new File (fname);
System.out.println ("File name: " + f.getName ());
System.out.println ("Path:"+ f.getPath ());
System.out.println ("Absolute Path:"+ f.getAbsolutePath ());
System.out.println ("Parent:"+ f.getParent ());
System.out.println ("Exists:"+ f.exists ());
if ( f.exists() )
{
System.out.println ("Is writable: "+ f.canWrite ());
System.out.println ("Is readable: "+ f.canRead ());
System.out.println ("Is executable: "+ f.canExecute ());
System.out.println ("Is directory: "+ f.isDirectory());
System.out.println ("File size in bytes: "+ f.length());
}
}
}