Create intermediate folders if one doesn't exist

后端 未结 3 579
别跟我提以往
别跟我提以往 2020-12-24 10:46

I am trying to create a folder for each username a user logs in as. Currently I have

private String destination = \"C:/Users/Richard/printing~subversion/file         


        
相关标签:
3条回答
  • 2020-12-24 11:19

    You have to actually call some method to create the directories. Just creating a file object will not create the corresponding file or directory on the file system.

    You can use File#mkdirs() method to create the directory: -

    theFile.mkdirs();
    

    Difference between File#mkdir() and File#mkdirs() is that, the later will create any intermediate directory if it does not exist.

    0 讨论(0)
  • 2020-12-24 11:30

    A nice Java 7+ answer from Benoit Blanchon can be found here:

    With Java 7, you can use Files.createDirectories().

    For instance:

    Files.createDirectories(Paths.get("/path/to/directory"));
    
    0 讨论(0)
  • 2020-12-24 11:33

    Use this code spinet for create intermediate folders if one doesn't exist while creating/editing file:

    File outFile = new File("/dir1/dir2/dir3/test.file");
    outFile.getParentFile().mkdirs();
    outFile.createNewFile();
    
    0 讨论(0)
提交回复
热议问题