Does anyone know how to use Java to create sub-directories based on the alphabets (a-z) that is n levels deep?
/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
开发者_如何学Python /b
..
You can simply use the mkdirs() method of java.io.File
class.
Example:
new File("C:\\Directory1\\Directory2").mkdirs();
If you don't mind relying on a 3rd party API, the Apache Commons IO package does this directly for you. Take a look at FileUtils.ForceMkdir.
The Apache license is commercial software development friendly, i.e. it doesn't require you to distribute your source code the way the GPL does. (Which may be a good or a bad thing, depending on your point of view).
Since Java 7, java.nio is preferred
import java.nio.file.Files;
import java.nio.file.Paths;
...
Files.createDirectories(Paths.get("a/b/c"));
public static void main(String[] args) {
File root = new File("C:\\SO");
List<String> alphabet = new ArrayList<String>();
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<String> 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
recusively creates a depth
-level directory tree based on a given list of String
s, which, in the case of main
, consists of a list of characters in the English alphabet.
If you're using mkdirs()
(as suggested by @Zhile Zou) and your File
object is a file and not a directory, you can do the following:
// Create the directory structure
file.getParentFile().mkdirs();
// Create the file
file.createNewFile();
If you simply do file.mkdirs()
it will create a folder with the name of the file.
I would write a little utility method that takes the start and the end letter as well as the desired deepth as parameters. This method calls itself recursively till done:
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);
}
}
One would call it like this:
createAlphabetFolders(new File("abctest"), 'A', 'E', 5);
Scala code:
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 _ =>
}
}
}
}
You could use three loops over characters a-z
like so:
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++;
}
}
}
This simply outputs the paths to the console. You could use File#mkdirs()
to create a recursive directory structure or create the directories in each intermediate part of the nested loop. I'll leave that for you to finish.
// ensures parent directory is created
File parentFile = destFile.getParentFile();
if (parentFile != null && !parentFile.exists()) {
parentFile.mkdirs();
}
// creates destination file
if (!destFile.exists()) {
destFile.createNewFile();
}
Apache commons addresses most of these. Try -
org.apache.commons.io.FileUtils.forceMkdir(directory);
精彩评论