Home >Java >JavaBase >file.delete() in java cannot delete files

file.delete() in java cannot delete files

王林
王林Original
2020-02-04 16:13:314803browse

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

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!

Statement:
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