testdirop.java

来自「一个很好的Java函数实例」· Java 代码 · 共 37 行

JAVA
37
字号
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 + =
减小字号Ctrl + -
显示快捷键?