Thursday, January 14, 2010

File Class of Java

In Java, the use of File class is possible. One can actually check information about the file like its existence, modification dates, and size.

I have the program below to check whether a certain file exists.  It is however, important that if there really is a file, the location path must be correct.

[+/-] show/hide

public class checkFile{
public static void main(String args[]){
File f=new File("C:\\Documents and Settings\\Rose\\My Documents\\NetBeansProjects\\SampleExercises\\src\\","data.txt");
if (f.exists())
System.out.println(f.getName()+" exists.");
System.out.println("The file is "+f.length()+" bytes long" );
if (f.canRead())
System.out.println(" Ok to read.");
if (f.canWrite())
System.out.println(" ok to write.");
System.out.println("File does not exist.");

No comments: