How to create a directory and sub directory structure with java?

后端 未结 5 1434
忘了有多久
忘了有多久 2020-12-29 05:39

Hello there I want to create the directories and sub directories with the java. My directory structure is starts from the current application directory, Means in current pro

5条回答
  •  情深已故
    2020-12-29 06:44

    You could do it with File#mkdirs() and something like,

    // The "/" is cross-platform safe as a path-separator in Java.
    // So is "\\" but that's twice the characters!
    String path = createImages.getAbsolutePath() + "/Images";
    File f = new File(path);
    if (!f.isDirectory()) {
      boolean success = f.mkdirs();
      if (success) {
        System.out.println("Created path: " + f.getPath());
      } else {
        System.out.println("Could not create path: " + f.getPath());
      }
    } else {
      System.out.println("Path exists: " + f.getPath());
    }
    

    Per the linked Javadoc,

    Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.

提交回复
热议问题