How to use file. XML in Java Mkdirs () writes elegant code?

I need a directory to save the file, but I'm not sure if it exists

File saveDir = new File("/tmp/appname/savedir/");
if(!saveDir.exists()){
    saveDir.mkdirs(); 
}

As mentioned above, there is a problem The method "savedir. Exists()" returns a Boolean value indicating whether the file path exists

Of course, I can write some redundant and ugly code to work

Is there any way to write elegant code to achieve this goal?

Solution

Is your question about "redundant" code to check the existence of directories, or do you need to create a directory and all missing parent directories?

I believe that using FileUtils in Apache commons IO can easily accomplish these two tasks:

FileUtils.forceMkDir(new File("/some/missing/parent/directory/foo"));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>