The delete() method of Java's File class deletes a file, and the isFile() method of Java's File class checks whether a certain value is a file.

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:

  1. Instantiate a File object and pass in the value to be checked.

  2. Call the File object's isFile() method, which returns a Boolean value indicating whether the value is a file.

  3. 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("这不是一个文件");
}

Guess you like

Origin blog.csdn.net/m0_73358221/article/details/129796081