递归创建目录
有没有人知道如何使用Java创建基于n级深度字母表(az)的子目录?
/a /a /a /b /c .. /b /a /b .. .. /a /b /c .. /b /a /a /b .. /b /a /b .. .. /a /b .. .. /a /a /b .. /b /a /b .. .. /a /b ..
public static void main(String[] args) { File root = new File("C:\\SO"); List alphabet = new ArrayList (); for (int i = 0; i < 26; i++) { alphabet.add(String.valueOf((char)('a' + i))); } final int depth = 3; mkDirs(root, alphabet, depth); } public static void mkDirs(File root, List dirs, int depth) { if (depth == 0) return; for (String s : dirs) { File subdir = new File(root, s); subdir.mkdir(); mkDirs(subdir, dirs, depth - 1); } }
mkDirs
根据给定的String
列表重新创建depth
级目录树,在main
的情况下,它由英文字母表中的字符列表组成。
您可以简单地使用java.io.File
类的mkdirs()方法。
例:
new File("C:\\Directory1\\Directory2").mkdirs();
如果您不介意依赖第三方API, Apache Commons IO软件包将直接为您执行此操作。 看看FileUtils.ForceMkdir 。
Apache许可证是商业软件开发友好的,即它不要求您像GPL那样分发源代码。 (这可能是好事也可能是坏事,取决于你的观点)。
我会写一个小实用程序方法,它将开始和结束字母以及所需的深度作为参数。 此方法以递归方式调用自身,直到完成:
private static void createAlphabetFolders(File parent, int start, int end, int deepth){ if(deepth <= 0){ return; } for (int i=start; i < end; i++){ // create the folder String folderName = "" + ((char) i); File folder = new File(parent, folderName); System.out.println("creating: " + folder.getPath()); folder.mkdirs(); // call recursively createAlphabetFolders(folder, start, end, deepth-1); } }
有人会这样称呼它:
createAlphabetFolders(new File("abctest"), 'A', 'E', 5);
Groovy有FileTreeBuilder类。 问题是,它的描述很弱,例子有错误。 所以,我还没有看到任何使用它的代码。 正如我所发现的,如果不设置baseDir字段,它将无法正常工作。 也许,它会解决你的问题。
def tree = new FileTreeBuilder() tree.src { main { groovy { 'Foo.groovy'('println "Hello"') } } test { groovy { 'FooTest.groovy'('class FooTest extends GroovyTestCase {}') } } }
这是docs的例子。 但它只有在你设置baseDir时才会起作用。 例如,通过构造函数参数传递。
Scala代码:
def makePathRecursive(path: String) = { import java.io.File import scala.util.{Try, Failure, Success} val pathObj = new File(path) pathObj.exists match { case true => // do nothing case false => Try(pathObj.mkdirs) match { case Success(_) => // it worked case Failure(e) => // maybe created meanwhile by another thread pathObj.exists match { case false => throw new Exception(e) case _ => } } } }
Apache commons解决了其中大部分问题。 试试 –
org.apache.commons.io.FileUtils.forceMkdir(目录);
您可以在字符az
使用三个循环,如下所示:
import java.io.*; public class FileCreate { public static void main(String[] args) throws Exception { char trunkDirectory = 'a'; char branchDirectory = 'a'; char leaf = 'a'; while (trunkDirectory != '{') { while (branchDirectory != '{') { while (leaf != '{') { System.out.println(trunkDirectory + "/" + branchDirectory + "/" + leaf++); } leaf = 'a'; branchDirectory++; } branchDirectory = 'a'; trunkDirectory++; } } }
这只是输出到控制台的路径。 您可以使用File#mkdirs()
创建递归目录结构,或在嵌套循环的每个中间部分创建目录。 我会留给你完成。
// ensures parent directory is created File parentFile = destFile.getParentFile(); if (parentFile != null && !parentFile.exists()) { parentFile.mkdirs(); } // creates destination file if (!destFile.exists()) { destFile.createNewFile(); }
- java.util.Objects.isNull vs object == null
- 使用JDBC在SQL中的日期之间进行搜索?
- 在src目录中的eclipse中添加文件夹而不使其成为包
- 在Hibernate中持久化Joda DateTime而不是Java Date
- IntelliJ IDEA 13.1 – 如何使用3GB RAM防止32位操作系统(Win 7)上的内存不足问题
- 同一个包的类可以分布在多个Jar文件中吗?
- JavaFX Circle和ImageView
- 如何在WEB-INF / class文件夹下获取资源路径(java ee web dynamic project)
- Resteasy和fileupload:没有内容处理错误