File cannot be deleted while Java calls file deletion method Delete () (file stream is closed and still cannot be deleted)

Source: Internet
Author: User

The JVM occupies a file that causes the file not to be deleted, and attempting to manually delete the file during debug will prompt "The file has been opened in Java platform se binary."

Generally speaking, there are several reasons for Java File.delete failure
1. See if it is referenced by another process, delete it manually (delete is occupied by another process)
2.file is a folder and is not empty, a different folder or file,
3. There is a high likelihood that the flow of this file will not be closed before itself
Reproduced from: https://www.cnblogs.com/stono/p/6736767.html

But after I added the file flow shutdown statement, I still didn't solve my problem. Speculation may be the way the flow closed, only to think that should be in the closed flow in finally to be reliable, to find books found in the book is not very good, and then found in the try to close the file flow in the correct posture. (Thank you: https://blog.csdn.net/qq_27093465/article/details/52439754) The stream is declared in a try, initialized in a try, and then in finally to close, Remember to handle the exception e,finally inside close and try a catch again.

 private static void Testclosefilestream () {final Logger LOG = Loggerfactory.getlogge  
       R (Cmshome.class);  
       String fileName = "";  
           InputStream InputStream = null;//declares a reference because the new object is also an exception to try {///here, if the filename does not exist.  
       InputStream = new FileInputStream (fileName);  
           catch (IOException e) {///This is mainly to show the exception to see, or even if abnormal, see can not find the problem.  
       Log.debug ("Loadproperties ioexception:" + e.getmessage ()); finally {if (InputStream!= null) {try {inputstream.close ();//Close Stream} catch (IOException e) {log.debug ("InputStream close IOException:" + E.getmessag  
               E ()); }  
           }  
       }  
   }  
  }
Related Article

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.