程序中用到的方法描述
CreateNewFile() 函数用于新建文件
注意事项:
1.文件若已经存在,则不再创建,返回false;
2.文件路径不存在,则会报错:IOException:系统找不到指定的路径Mkdirs() 函数用于新建多层文件夹 delete() 函数用于删除文件,删除空文件
public class Test {
public static void main(String[] args) throws IOException {
System.out.println("请输入文件路径(格式:d://abc//a//b//c):");
String a = new Scanner(System.in).nextLine();
File f1 = new File(a);
f1.mkdirs();
System.out.println("请输入创建的文件名称:");
String b = new Scanner(System.in).nextLine();
File f2 = new File(f1, b);
f2.createNewFile();
System.out.println("请按回车删除文件"+ b);
new Scanner(System.in).nextLine();
boolean q = f2.delete();
if(true == q) {
System.out.println("删除成功");
}else {
throw new RuntimeException("出错了");
// 可以将文件名修改,就可以测试报错
}
String re = a.substring(a.lastIndexOf("/")+1);
System.out.println("请回车删除"+re+"文件夹");
new Scanner(System.in).nextLine();
f1.delete();
System.out.println("删除成功");
}
Console:
文件夹这里就不截图了,自行测试