如何用java创建目录和子目录结构?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25174784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:47:40  来源:igfitidea点击:

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

java

提问by Gopal00005

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 projects directory which looks like following...

你好,我想用 java 创建目录和子目录。我的目录结构是从当前应用程序目录开始的,在当前项目目录中的意思是这样的......

Images
   |
   |+ Background
   |
   |+ Foreground
          |
          |+Necklace
          |+Earrings
          |+Etc...

I know how to create directory but I need to create sub directory I tried with following code what should be next steps ?

我知道如何创建目录但我需要创建子目录我尝试使用以下代码下一步应该做什么?

File file = new File("Images");
file.mkdir();

采纳答案by Michael Aaron Safyan

You can use File.mkdir()or File.mkdirs()to create a directory. Between the two, the latter method is more tolerant and will create all intermediate directories as needed. Also, since I see that you use "\\" in your question, I would suggest using File.separatorfor a portable path separator string.

您可以使用File.mkdir()File.mkdirs()创建目录。在两者之间,后一种方法更宽容,将根据需要创建所有中间目录。另外,由于我看到您在问题中使用了 "\\",我建议使用File.separator作为便携式路径分隔符字符串。

回答by Elliott Frisch

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

你可以用File#mkdirs()类似的东西来做,

// 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,

根据链接的 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.

创建由此抽象路径名命名的目录,包括任何必要但不存在的父目录。请注意,如果此操作失败,则它可能已成功创建了一些必要的父目录。

回答by Chandrasekhar Rajoli

You can create all parent directories by using File.mkdirs().

您可以使用File.mkdirs()创建所有父目录。

File.mkdirs() - 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.

File.mkdirs() - 创建以此抽象路径名命名的目录,包括任何必要但不存在的父目录。请注意,如果此操作失败,则可能已成功创建了一些必要的父目录。

回答by ROMANIA_engineer

Starting from Java 7, you can use the java.nio.file.Files& java.nio.file.Pathsclasses.

Java 7开始,您可以使用java.nio.file.Files&java.nio.file.Paths类。

Path path = Paths.get("C:\Images\Background\..\Foreground\Necklace\..\Earrings\..\Etc");

try {
    Files.createDirectories(path);
} catch (IOException e) {
    System.err.println("Cannot create directories - " + e);
}

This is a tricky solution(because I used only one path to go to the whole structure).

这是一个棘手的解决方案(因为我只使用了一条路径来访问整个结构)。

If you don't like tricky solutions, you can use 4 simple paths instead:

如果你不喜欢棘手的解决方案,你可以使用 4 个简单的路径:

Path p1 = Paths.get("C:\Images\Background");
Path p2 = Paths.get("C:\Images\Foreground\Necklace");
Path p3 = Paths.get("C:\Images\Foreground\Earrings");
Path p4 = Paths.get("C:\Images\Foreground\Etc");

and then call the createDirectoriesmethod for all of them:

然后createDirectories为所有人调用该方法:

Files.createDirectories(p1);
Files.createDirectories(p2);
Files.createDirectories(p3);
Files.createDirectories(p4);

回答by Rupesh tiwari

You can just use file.mkdirs(), it will create sub-directory.

您可以只使用file.mkdirs(),它会创建子目录。

String path = images + File.separator + Background + File.separator + Foreground + File.separator + Necklace + File.separator  + Earrings ;
File file = new File( path );
file.mkdirs();