Home > Java > Java Basics > body text

file.delete() in java cannot delete files

王林
Release: 2020-02-04 16:13:31
Original
4566 people have browsed it

file.delete() in java cannot delete files

1. Problem

file.delete() cannot delete the file. file.delete() cannot delete the file. The result is false no matter how many times it is executed.

2. Analysis of the reasons

(1) There may be a stream operating on the file, or the stream operation is not closed after the stream operation is completed;

(2) It is possible that an external process or program is occupying the file.

(Free learning video sharing: java video tutorial)

3. Solution

Method 1: If it can be checked If the above problem occurs, close the relevant stream or process, and the problem will naturally be solved.

Method 2: (Ultimate method!) Directly pass new File ("absolute path of the file"); then execute file.delete() to successfully delete.

The specific examples are as follows:

File file = new File("D:/a.jpg");      
file.delete();         //返回结果就是true
Copy after login

Related article tutorial sharing: java introductory tutorial

The above is the detailed content of file.delete() in java cannot delete files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!