Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the solution that Java cannot delete occupied resource files

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report