Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

...

Noncompliant Code Example (delete())

Wiki MarkupThis noncompliant code example attempts to delete a specified file but gives no indication of its success. The Java Platform, Standard Edition 6 API Specification \[ [API 2006|AA. References#API 06] \] requires {{File.delete()}} to throw a {{SecurityException}} only when the program lacks authorization to delete the file. No other exceptions are thrown, so the deletion can silently fail.

Code Block
bgColor#FFCCCC
File file = new File(args[0]);
file.delete();

...

Code Block
bgColor#ccccFF
Path file = new File(args[0]).toPath();
try {
  Files.delete(file);
} catch (IOException x) {
  System.out.println("Deletion failed");
  // handle error
}

Wiki MarkupThe Java SE 7 Documentation \[ [J2SE 2011|AA. References#J2SE 11]\] defines {{Files.delete()}} to throw the following exceptions:

Exception

Reason

NoSuchFileException

File does not exist

DirectoryNotEmptyException

File is a directory and could not otherwise be deleted because the directory is not empty

IOException

An I/O error occurs

SecurityException

In the case of the default provider and a security manager is installed, the SecurityManager.checkDelete(String) method is invoked to check delete access to the file

...

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="523ec875-257b-4f07-9d7f-3a4499210240"><ac:plain-text-body><! [CDATA[ [[API 2006AA. References#API 06] ]

File.delete()

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="40eb997d-a58f-428f-ae5a-14c11d5caa02"><ac:plain-text-body><! [CDATA[ [[J2SE 2011AA. References#J2SE 11] ]

Files.delete()]]></ac:plain-text-body></ac:structured-macro><ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="37973aca-2f13-4876-b2af-910351a15f37"><ac:plain-text-body><![CDATA

[ [[Seacord 2005AA. References#Seacord 05]]

Chapter 7, File I/O ]]></ac:plain-text-body></ac:structured-macro>

...

      12. Input Output (FIO)