如何在给定文件夹名称的情况下创建多个目录 [英] How to create multiple directories given the folder names
问题描述
我有一个文件列表,这些文件的名称由一个类组和一个id (例如science_000000001.java)
I have a list of files, the names of these files are are made of a classgroup and an id (eg. science_000000001.java)
我能够获取所有文件的名称并将其拆分,因此我将类组放入一个数组,将id放在另一个数组中。相同的值。
i am able to get the names of all the files and split them so i am putting the classgroups into one array and the ids in another.. i have it so that the arrays cant have two of the same values.
这是问题,我想用这些类组和ID创建一个目录,例如:
This is the problem, i want to create a directory with these classgroups and ids, an example:
science_000000001.java would be in science/000000001/science_000000001.java
science_000000002.java would be in science/000000002/science_000000002.java
maths_000000001.java would be in maths/000000001/maths_000000001.java
但我想不出一种方法来正确遍历数组以创建适当的目录?
but i cannot think of a way to loop through the arrays correctly to create the appropriate directories?
我也可以自己创建文件夹,只是获得正确的目录是问题所在,有人有什么想法吗?
Also i am able to create the folders myself, its just getting the correct directories is the problem, does anyone have any ideas?
推荐答案
给出:
String filename = "science_000000001.java";
然后
File fullPathFile = new File(filename.replaceAll("(\\w+)_(\\d+).*", "$1/$2/$0"));
为您提供文件的完整路径,在这种情况下为 science / 000000001 /science_000000001.java
gives you the full path of the file, in this case science/000000001/science_000000001.java
如果要创建目录,请使用以下命令:
If you want to create the directory, use this:
fullPathFile.getParentFile().mkdirs();
这篇关于如何在给定文件夹名称的情况下创建多个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!