Recursively create directory

前端 未结 10 667
萌比男神i
萌比男神i 2020-12-30 23:20

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
           


        
10条回答
  •  清酒与你
    2020-12-30 23:43

    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.

提交回复
热议问题