Credits

Thursday, January 13, 2011

Another Program on Java's File Class

In my previous post, we used File class to create a File object. File class is used to display key information on the file associated with the class. 

For instance, if we have the instantiation  of File as indicated below, a physical file must exist to link it to the created object.:

File file = new File("\\Documents and Settings\\Rose\\My Documents\\NetBeansProjects\\SampleExercises\\src\\myuyabs.txt");


[+/-] show/hide

import java.io.*;
public class FileDeleteSample {

private static void doCreate() {

// Create a File object
File file = new File("\\Documents and Settings\\Rose\\My Documents\\NetBeansProjects\\SampleExercises\\src\\myuyabs.txt");

if(!file.exists())
System.out.println("file not found.");
else
{
boolean success = file.delete();

if (success) {
System.out.println("File was successfully deleted.\n");
} else {
System.out.println("File was not successfully deleted.\n");
}
}
}


/**
* Sole entry point to the class and application.
* @param args Array of String arguments.
*/
public static void main(String[] args) {
doCreate();
}

}

AddThis