Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It returns a <code>boolean</code> value, you should check that. From the <a href="http://docs.oracle.com/javase/7/docs/api/java/io/File.html#delete%28%29">JavaDoc</a>:</p> <blockquote> <p>Returns: true if and only if the file or directory is successfully deleted; false otherwise</p> </blockquote> <p>You should check the value of the return and take action.</p> <p>If it returns <code>false</code> it may well be that you do not have permission to delete the file.</p> <p>In that case you can check whether the file is writeable by the application and if not attempt to make it writeable - again this returns a <code>boolean</code>. If successful you can try deleting again.</p> <p>You could use a utility method:</p> <pre><code>private void deleteFile(final File f) throws IOException { if (f.delete()) { return; } if (!f.canWrite() &amp;&amp; !f.setWritable(true)) { throw new IOException("No write permissions on file '" + f + "' and cannot set writeable."); } if (!f.delete()) { throw new IOException("Failed to delete file '" + f + "' even after setting writeable; file may be locked."); } } </code></pre> <p>I would also take their advice in the JavaDoc:</p> <blockquote> <p>Note that the Files class defines the delete method to throw an IOException when a file cannot be deleted. This is useful for error reporting and to diagnose why a file cannot be deleted.</p> </blockquote> <p>Provided that you are using Java 7 that is. That method throws a number of exceptions that you can handle:</p> <pre><code>try { Files.delete(path); } catch (NoSuchFileException x) { System.err.format("%s: no such" + " file or directory%n", path); } catch (DirectoryNotEmptyException x) { System.err.format("%s not empty%n", path); } catch (IOException x) { // File permission problems are caught here. System.err.println(x); } </code></pre> <p>Example taken from the <a href="http://docs.oracle.com/javase/tutorial/essential/io/delete.html">Oracle tutorial page</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload