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

The solution to delete files and folders in Java

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is to share with you about the solutions to delete files and folders in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The problem lies.

Today, when I am writing a project, the process is to first create a temporary folder to store the generated files, then compress and export it, and then delete the temporary folder after the export is successful, but I still think that there is something wrong with the write deletion method. I've tried a lot of methods, but I can't delete it.

Originally thought to be the packaged stream did not close, but found that the stream is closed, later found that it is in the generated method, directly new FileWriter out, not closed, here to remind you, about the operation of files or folders must use the stream, so the stream must be used to check whether it is closed.

Pits that cannot be deleted from java files, especially compressed files

The file.delete (); method is usually called directly when we delete a file with java, but in many cases it cannot be deleted.

Here are some examples

1. It is a folder and is not empty. There is nothing to say about this. You can delete the files in the folder first and then delete the folder.

two。 Occupied by other processes, not only java can not be deleted, but also windows cannot be deleted manually.

3. The stream with this file is not closed, which is the most common reason.

However, I encountered uploading a file, and then the stream associated with the file was also closed, still could not be deleted, and later found that someone secretly converted the file into a zip compressed file.

Then delete the bookFile and cannot delete it alive. If this happens, the zipFile must be close in the end.

The java.util.zip.ZipFile.close () method closes the ZIP file. Closing this ZIP file closes all input streams previously returned by calling the getInputStream () method. Even the stream of this method has been closed to us! We don't have to manually close the stream of the zip file. Finally, we can delete the file by calling the file.delete () method.

Thank you for reading! On the "Java delete files and folders can not be deleted solutions," this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

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

Development

Wechat

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

12
Report