First, a few of the files can be deleted and delete the file (the Test1.txt file is created in the F disk, and then you can copy the code directly to the IDE), and finally summarize the following reasons:
Example one: The following example is undoubtedly able to delete the file
Import Java.io.File; Import java.io.IOException; Public class Test { publicstaticvoidthrows IOException { New File ("F:/test1.txt"); File.delete (); }}
Cause: An in-process (or thread) single thread execution, there is no resource sharing problem, so can be deleted.
Example two: The following example will delete the success, but the exception will be reported after the file is not found
ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream; Public classTest { Public Static voidMain (string[] args)throwsIOException {fileoutputstream fos=NULL; File File=NewFile ("F:/test1.txt"); if(!file.exists ()) {File.createnewfile (); //construct write file contentsFOS =Newfileoutputstream (file); Fos.write ("Hello wolrd". GetBytes ()); } File.delete (); InputStream InputStream=Newfileinputstream (file); }}
Cause: Even if the subsequent inputstream is used to file, but the Delete method at the red line has deleted the files, the exception to the system file is reported. Then look at the following example.
Example three: The following example will delete the failure because InputStream is using the File,io stream has not been closed
ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream; Public classTest { Public Static voidMain (string[] args)throwsIOException {fileoutputstream fos=NULL; File File=NewFile ("F:/test1.txt"); if(!file.exists ()) {File.createnewfile (); //construct write file contentsFOS =Newfileoutputstream (file); Fos.write ("Hello wolrd". GetBytes ()); } InputStream InputStream=Newfileinputstream (file); File.delete (); }}
The deletion failed because the subsequent inputstream was used to file, and the InputStream did not close the stream, causing the deletion to fail.
Example four: The following example will delete the success, the reason is that the latter InputStream first close IO stream and then delete
ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream; Public classTest { Public Static voidMain (string[] args)throwsIOException {fileoutputstream fos=NULL; File File=NewFile ("F:/test1.txt"); if(!file.exists ()) {File.createnewfile (); //construct write file contentsFOS =Newfileoutputstream (file); Fos.write ("Hello wolrd". GetBytes ()); } InputStream InputStream=Newfileinputstream (file); //Close the stream if(InputStream! =NULL) {inputstream.close (); } File.delete (); }}
Cause: The deletion succeeds because the latter InputStream first closes the IO stream and then calls the Delete method to remove the file, which can be different from the example of the three comparisons.
Here are some things to note about the Delete method of file:
1. If the file to be deleted is being occupied, the file will not be deleted, for example, the other program is still reading the file, not enough time to release the file, the following program will attempt to delete the file, will cause the deletion failed.
2. In the process of using IO to remember to close the flow, this is the most basic quality of the programmer.
The Delete method of the file class of Java causes Analysis to delete files