📄 testdirop.java
字号:
package apibook.c3.s5.File;import java.io.File;//测试File的目录操作,创建和删除功能public class TestDirOp { public TestDirOp() { } public static void main(String[] args) { String filepath = "TestDirOp"; File dir = new File(filepath); if (dir.exists()) {//如果存在该目录,则删除 System.out.println((dir.delete() ? "Deleted " : "Could not delete ") + dir.getPath()); }//不存在则建立该目录 if (dir.mkdirs()) {//建立该目录 System.out.println("Created directory " + dir.getAbsolutePath()); File subdir = new File(dir, "newSub");//在该目录下建立新的目录 if (subdir.mkdir()) { System.out.println("Created subdirectory " + subdir.getAbsolutePath());//建立 System.out.println((subdir.delete() ? "Deleted " : "Could not delete ") + subdir.getPath());//删除 } else//建立子目录不成功 System.out.println("Could not create subdirectory " + subdir.getAbsolutePath()); System.out.println((dir.delete() ? "Deleted " : "Could not delete ") + dir.getPath());//删除该目录 } else {//不能建立该目录 System.out.println("Could not create directory " + dir.getAbsolutePath()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -