mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-10 17:49:38 +02:00
Add FileUtils.maybeCreateFileWithParentDirectories(File)
This commit is contained in:
parent
a70ae7ab8e
commit
fb3009adb2
3 changed files with 26 additions and 28 deletions
|
@ -207,4 +207,28 @@ public final class FileUtils {
|
|||
throw new IOException("Could not delete file " + file);
|
||||
}
|
||||
}
|
||||
|
||||
public static void maybeCreateFileWithParentDirectories(File file) throws IOException {
|
||||
File parent = file.getParentFile();
|
||||
if (!parent.exists() && !parent.mkdirs()) {
|
||||
throw new IOException("Cannot create directory " + parent);
|
||||
}
|
||||
|
||||
if (file.isFile()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!file.exists()) {
|
||||
if (file.createNewFile()) {
|
||||
return;
|
||||
}
|
||||
throw new IOException("Cannot create file " + file);
|
||||
}
|
||||
|
||||
if (file.isDirectory()) {
|
||||
throw new IOException("File " + file + " exists, but is a directory.");
|
||||
} else {
|
||||
throw new IOException("File " + file + " exists, but is neither a file nor a directory");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue