The following static methods can be used to easily delete a file. Different arguments allow a flexible call with path string or file object.
The path method expects for the first argument the file path as string. A FileNotFoundException will be thrown if the file does not exist.
public static void delete(String path)
throws FileNotFoundException, IOException
{
delete(new File(path));
}
The ultimate method for deleting gets the file path in a file object. All appearing exceptions will be outreach to the outside and thus can either be catch directly in the calling method or in a global exception routine.
public static void delete(File file)
throws FileNotFoundException, IOException
{
if (file.exists() == false)
throw new FileNotFoundException("File does not exist! " + file);
if (file.delete() == false)
throw new IOException("File could not be deleted! " + file);
}
delete("test.txt");
delete(file);
Author: Johannes HammoudComments Date: 10.09.2009