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