Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically create a missing folder? [duplicate]

Tags:

java

file-io

In java, I need to write a string into a new file, like 'c:\test\upload\myfile.txt', if the 'upload' folder is not existing, it will automatically create it. how to do it ? Apache Commons IO has this API ?

like image 343
user595234 Avatar asked May 14 '12 14:05

user595234


1 Answers

In addition to the accepted answer, since the question also mentioned the library Apache Common IO, I report in the following a solution by using this nice library:

File file = new File("...  the directory path ..."); 
FileUtils.forceMkdir(file);

This solution uses the class FileUtils, from package org.apache.commons.io and the method forceMkdir, that "Makes a directory, including any necessary but nonexistent parent directories".

like image 192
JeanValjean Avatar answered Oct 18 '22 15:10

JeanValjean