package com.teach._15_IO流._File类;
import java.io.File;
/**
* 类功能描述:二、删除文件或目录
* 'boolean delete()': 删除文件或目录,如果目录为空则无法删除
*
* @author GongziYu
* @date 2024/08/19 19:23
*/
public class Test02_ {
public static void main(String[] args) {
//删除文件
File file = new File("example.txt");
if (file.delete()){
System.out.println("File delete: " + file.getName());
}else {
System.out.println("Failed to delete the file");
}
//删除目录
File directory = new File("exampleDir");
if (directory.delete()){
System.out.println("Directory delete: " +directory.getName());
}else {
System.out.println("Failed to delete the directory.");
}
}
}
输出
Failed to delete the file
Failed to delete the directory.