The Delete method of the file class of Java causes Analysis to delete files

Source: Internet
Author: User

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

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.