File cannot be deleted java.io.IOException:Unable to delete

Source: Internet
Author: User

Question: 1. The return value is False when the File.delete () method is called.

2. Why call Files.move (from,to) in the Guava tool jar package to report an exception: Java.io.IOException:Unable to delete

You need to create a test.txt file before executing the code program.

On the code:

 Packageindi.johnny.test007;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.InputStream; Public classDemo { Public Static voidMain (string[] args) {Try{File File=NewFile ("C:/users/johnny/desktop/test.txt"); if(File.exists ()) {InputStream InputStream=Newfileinputstream (file);                Inputstream.close (); BooleanFlag =File.delete ();            SYSTEM.OUT.PRINTLN (flag); }        } Catch(Exception e) {System.out.println (e); }                    }    }

Executing the above code will print "false", which means the deletion of the file failed.

But the comment line in the above code is "Inputstream.close ();" Open, print out "true" to delete the file successfully.

Conclusion: The data stream loaded by the file InputStream, if not closed, the file cannot be deleted.

The Files.move (from,to) method in guava has a call to the File.delete () method, so when InputStream does not execute the close () method, the exception "Unable to delete" is reported.

The following is the contents of the Files.move (from,to) method in Guava:

 Public Static voidmove (file from, file to)/*     */     throwsIOException/*     */   {/*494*/Preconditions.checknotnull (from);/*495*/Preconditions.checknotnull (to);/*496*/Preconditions.checkargument (!from.equals (To), "Source%s and destination%s must is different",Newobject[] {from, to});/*     */     /*     */ /*499*/     if(!From.renameto (To)) {/* -*/copy (from, to);/*501*/       if(!From.delete ()) {/*502*/         if(!To.delete ()) {/*503*/           Throw NewIOException ("Unable to delete" +to );/*     */         }/*505*/         Throw NewIOException ("Unable to delete" +from );/*     */       }/*     */     }/*     */}

File cannot be deleted java.io.IOException:Unable to delete

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.