9 Replies Latest reply: Mar 18, 2010 9:44 PM by 3004 RSS

    Deleting a file Exception

    843789
      Hello,

      Im using code like the below....
      File dir = new File(newDir);
           
      
               // Make sure the file or directory exists and isn't write protected
               if (!dir.exists())
                 throw new IllegalArgumentException(
                     "Delete: no such file or directory: " + fileName);
      
               if (!dir.canWrite())
                 throw new IllegalArgumentException("Delete: write protected: "
                     + fileName);
      
               // Attempt to delete it if already exists
               if(newFile.exists()){
                    boolean success = newFile.delete();
                     if (!success)
                          throw new IllegalArgumentException("Delete: deletion failed");
      }
      which throws the exception from the lasrt line, saying deletion failed. Im a little confused as I cant see why its fails, as it checks if it has permission to write to disk (which i presume is the same as permission to delete), and the file does exist (it checks for that also).

      Can anyone please enlighten me?

      Thanks :)