Search code examples
javadirectoryorganization

How to create multiple directories given the folder names


I have a list of files, the names of these files are are made of a classgroup and an id (eg. science_000000001.java)

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.

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?


Solution

  • Given:

    String filename = "science_000000001.java";
    

    Then

    File fullPathFile = new File(filename.replaceAll("(\\w+)_(\\d+).*", "$1/$2/$0"));
    

    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();