The delete() method of Java's File class deletes files. The sample code is as follows:
import java.io.File;
public class Demo8 {
public static void main(String[] args) {
File file = new File("path/to/file");
if (file.delete()) {
System.out.println("文件删除成功!");
} else {
System.out.println("文件删除失败!");
}
}
}
Among them, is the path of the file to be deleted. The delete() method returns true if the deletion is successful; otherwise it returns false. path/to/file
In Java, you can use the isFile() method of the File class to check whether a value is a file. The specific method is as follows:
-
Instantiate a File object and pass in the value to be checked.
-
Call the File object's isFile() method, which returns a Boolean value indicating whether the value is a file.
-
It is judged based on the returned Boolean value. If it is a file, it returns true, otherwise it returns false.
The sample code is as follows:
File file = new File("path/to/file");
if(file.isFile()) {
System.out.println("这是一个文件");
} else {
System.out.println("这不是一个文件");
}