5

In java any way to create a file without its parent foler and parent's parent folder

Here is the full path of the file to be created.D:\test3\ts435\te\util.log

There is not any folder existing in this path, which means there is nothing under D:\.

In java, when I create this file

File testFile=new File(filePath);
testFile.createNewFile();

It says it cannot find the path. Then I try to create the parent folder 'te'. Then it fail again, saying it cannot find the parent folder 'ts435'.

Is there any way to create the file forcely? To create the file with or without its parents and upper level folders exist.

Update 2019-06-28:

Hi guys, I finally find the reason. There are two mehtods, mkdir() and mkdirs(). When the destination folder's parent folder not exist, mkdir() will return false because it cannot forcely build the entire folder struncture.

However, mkdirs() can do this magic. It can build the entire folder chain whether parent folder exist or not.

Robin Sun
  • 1,352
  • 3
  • 20
  • 45

3 Answers3

10

You can ensure that parent directories exist by using this method File#mkdirs().

File f = new File("D:\\test3\\ts435\\te\\util.log");
f.getParentFile().mkdirs();
// ...

If parent directories don't exist then it will create them.

Mushif Ali Nawaz
  • 3,707
  • 3
  • 18
  • 31
  • When I create folder te, its parent ts435 does not exist. Then I have to create this first. And parent's parent... I have to loop this many times. I believe there is a simple way. – Robin Sun Jun 26 '19 at 08:03
  • @RobinSun if there is no folder in your D drive then this code will create following folders in this hierarchy: `D > test3 > ts435 > te`. You don't need to loop anything. – Mushif Ali Nawaz Jun 26 '19 at 08:07
  • 1
    @RobinSun The documentation for `mkdirs()` says (**emphasis** mine): "_Creates the directory named by this abstract pathname, **including any necessary but nonexistent parent directories**. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories._". – Slaw Jun 26 '19 at 08:26
  • @MushifAliNawaz, the f.getParentFile().mkdirs() returns false. This is werid. – Robin Sun Jun 26 '19 at 08:51
  • @RobinSun This is what documentation of `mkdirs()` say: Returns *true if and only if the directory was created, along with all necessary parent directories; **false otherwise***. Which implies that directory (along with parent directories) already exists and no new directories were created. – Mushif Ali Nawaz Jun 26 '19 at 08:53
  • @MushifAliNawaz Do you have any idea why the folder is not created? I run Intelli idea as administrator, so I guess there is no permission issue. – Robin Sun Jun 26 '19 at 08:55
0
File testFile=new File("D:\\test3\\ts435\\te\\util.log");

if(! testFile.getParentFile().exists()) {
    testFile.getParentFile().mkdirs();
}

testFile.createNewFile();
frianH
  • 7,295
  • 6
  • 20
  • 45
0

You can use following method to create file and directory together.

public static String createFile(String filePath, String fileName) throws BotServiceException {
        File directory = new File(filePath);
        if (!directory.exists() && !directory.mkdirs()) {
            throw new Exception("Directory does not exist and could not be created");
        }
        File newFile = new File(filePath+ File.separator + fileName);
        boolean isSuccess = newFile.createNewFile(); 
return newFile.getAbsolutePath();
}