In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "java delete files always return false how to do", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "java delete files always return to false how to do" it!
Catalogue
Java always returns false when deleting a file and cannot delete it.
My solution.
Pits that cannot be deleted from java files, especially compressed files
Here are some examples
The java.util.zip.ZipFile.close () method closes the ZIP file
Java always returns false when deleting a file and cannot delete it.
Today, I wrote a simple program to delete files: the code is as follows:
Public static boolean deleteFiles (File [] files) {boolean flag = false; for (File file:files) {/ / path is a file and is not empty, delete if (file.isFile () & & file.exists ()) {flag = file.delete (); System.out.println (flag);}} return flag;}
All my files exist, but I can't delete them often. I don't know what's going on. I can test them alone. I feel very strange. When I look them up on the Internet, they say there are possible situations:
1. Multithread operation
2. The open file stream is not closed
3. Need garbage collection
My solution.
According to the analysis of my situation, mine is single-threaded, so the first situation does not exist. In the second case, I close all my open file streams but it still doesn't work. Later, I added the line System.gc () to the program, and the result is really fine. The following is modified:
Delete public static boolean deleteFiles (File [] files) {boolean flag = false; for (File file:files) {/ / if the path is not empty if (file.isFile () & & file.exists ()) {System.gc (); / / add to make sure the file can be deleted, otherwise flag = file.delete (); System.out.println (flag);}} return flag may not be deleted } pits that cannot be deleted by 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.
2. It is occupied by other processes, which can not be deleted not only from java, but also from windows 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 your reading, the above is the "java delete files always return false how to do" of the content, after the study of this article, I believe you delete files on java always return to false how to do this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.