File Delete

The following static methods can be used to easily delete a file. Different arguments allow a flexible call with path string or file object.

Path String

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)); }

File Object

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); }

Call

delete("test.txt"); delete(file);

Resources

Johannes Hammoud File Delete 10.09.2009

I Coding : Community about Java programing

Language German+-

Java JSF JavaScript HTML CSS NetBeans GlassFish MySQL

Imprint

Visits
6279909
Today
7231