The reason for the delete () method of file in Java to remove files failed

Source: Internet
Author: User

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 you will not be able to close the stream of this file before yourself (the situation I encountered)

This method I used to get the size of the file, used the stream class, but ran out without shutting down, causing the file could not be deleted:

public string GetFileSize (string path,string filename) throws IOException {
String pathstring=path+ "\" +filename;
F=new File (pathstring);
FileInputStream fis=new FileInputStream (f);
String time=string.valueof ((double) fis.available ()/1024));
Fis.close ();//There was no shutdown.
Return time.substring (0, Time.indexof (".") +2) + "K";

}

Personally think: First, manually, that is, the computer found to delete the file to see if you can delete, if the deletion does not drop, (personal encounter is also the use of the file's input and output stream is not closed.) )


Check the input and output stream of your code to see if the power is used but not closed.





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.