File Properties in Java

Ram Pothuraju
This Simple Java Program is for Reading the Properties of File in Java. 

File Object in Java is used to to store files. The File object has many functions. The File object stores all the information about the file and has many functions to display the file properties.


The Below program opens a file named demofile.txt from a specified location (You can change the file you want to read). It then displays the various common properties of the file object.

import java.io.File;
import  java.lang.*;

class Character_file
{
public static void main(String ar[])
{
try
{
File f1= new File("D:/demofile.txt");
System.out.println("File Name:  "+ f1.getName());
System.out.println("File Path:  "+ f1.getPath());
System.out.println("File Absolute Path:   "+f1.getAbsolutePath());
System.out.println("Parent File:  "+f1.getParent());
System.out.println("File Exists?  "+f1.exists());
System.out.println("Can Write?  "+f1.canWrite());
System.out.println("Can Read?   "+f1.canRead());
System.out.println("Is Directory?  "+f1.isDirectory());
System.out.println("Is File?   "+f1.isFile());
System.out.println("Is Absolute?   "+f1.isAbsolute());
System.out.println("File last modified:  "+f1.lastModified());
System.out.println("File size:    "+f1.length());
}
catch(Exception ex)
{
System.out.println("Error occured");
}
}
}

Output:-

File Name:  demofile.txt
File Path:  D:\
File Absolute Path:   D:\demofile.txt
Parent File:  D:\
File Exists?  true
Can Write?  true
Can Read?   true
Is Directory?  false
Is File?   true
Is Absolute?   true
File last modified:  1072899408186
File size:    4 

Tags

Post a Comment

0Comments

Post a Comment (0)