Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the solution that Java cannot delete occupied resource files

Shulou Source: shulou.com Published: 2022-06-01 20:38:29 10月01日 Update

This article mainly introduces "what is the solution that Java cannot delete occupied resource files". In daily operation, I believe many people have doubts about what the solution is when Java cannot delete occupied resource files. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt that "Java cannot delete occupied resource files." Next, please follow the editor to study!

Recently, I am doing the function of a upload file, first upload the file to a temporary storage location, and then successfully upload the file to server, and after reading the data in the file, I need to delete the temporarily stored file, but sometimes I can and sometimes I can't delete the file using file.delete (). Then delete the file directly in the folder. The deletion error indicates that the operation cannot be completed because the file is already open in Java ™Platform SE binary.

The reason for this problem is that the created IO stream is not properly closed, resulting in the resource file being occupied all the time!

If the created IO stream is not closed, it will cause the file to be held by the stream all the time, so the deletion is not successful.

As long as you add the close method to each IO stream you create, you can use it, and remember that the stream created first closes last, and the creation closes one-to-one correspondence.

Speaking of which, I have to mention one point:

If there is no special need to create an IO stream, try not to use "anonymous inner classes" to create

Use the IO stream to read the file, and remember to close the stream at the end, otherwise it will hold the file all the time and consume resources.

At this point, the study on "what is the solution for Java unable to delete occupied resource files" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Files resources methods learning no success methods more sometimes problems help practical one-to-one correspondence next location function reason data folder article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Information Xiaomi Apple Shulou Technology