Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Java's file-manipulation methods often indicate failure with a return value rather than by throwing an exception. The Java TutorialTutorials for Java 7 notes:

Prior to the Java SE 7 release, the java.io.File class was the mechanism used for file I/O, but it had several drawbacks.

Many methods didn't did not throw exceptions when they failed, so it was impossible to obtain a useful error message. For example, if a file deletion failed, the program would receive a "delete fail" but wouldn't know if would not know whether it was because the file didn't existwas missing, the user didn't have permissionslacked sufficient permissions to access the file, or there was some other problem.

Consequently, programs that ignore the return values from file operations often fail to detect when that those operations have failed. Java programs must check the return values of methods that perform file I/O (this is a specific instance of rule EXP00-J. Do not ignore values returned by methods).

...

Wiki Markup
This 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. Bibliography#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("file"args[0]);
file.delete();

Compliant Solution

...

Code Block
bgColor#ccccFF
File file = new File("file");
if (!file.delete()) {
  System.out.println("Deletion failed");
}

Compliant Solution (Java

...

SE 7)

This compliant solution uses the Files.delete() method from Java 1.SE 7 to delete the file.

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

Wiki Markup
The Java SE 7 Documentation \[[J2SE 2011|AA. Bibliography#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

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d2295e483e2b7b5a-e0e33395-4c6b4c27-942198a7-0bdce3003cb9c14c12b5cbc6"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#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="5e0902a4ff711368-0b403ae9-46a5490a-a97cbfb5-d421f778b117f6fac8937eac"><ac:plain-text-body><![CDATA[

[[J2SE 2011

AA. Bibliography#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="95a9a50b8f8450e2-e6125bde-45b5495a-9c9ea222-14dc41e2224ec9a7f8cb05ef"><ac:plain-text-body><![CDATA[

[[Seacord 2005

AA. Bibliography#Seacord 05]]

Chapter 7, " File I/O "

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

...