FastFoodCoding

Login


Connect with one of the following accounts





Login

Please wait while we redirect...

Views: 447 Ask New Question


How can i make sure a directory exists in file system with the given path? For example if i provide a string like following

/a/b/c/d

How to write a Java program which creates these directories in case they does not exist ?

On By


1 Answer


You can use the following utility function which accepts a path and make sure that path exists in file system

    public static void ensureDirectory(String dirPath) {
        if (dirPath != null && !dirPath.isEmpty()) {
            File dir = new File(dirPath);
            if (!dir.exists()) {
                Path newDirPath = Paths.get(dirPath);
                if (!Files.exists(newDirPath)) {
                    try {
                        Files.createDirectories(newDirPath);
                    } catch (IOException e) {
                        throw new RuntimeException("Exception while creating directory: " + dirPath, e);
                    }
                }
            }
        }
    }

Following is a sample program which tests the above function

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Test {

    public static void main(String[] args) {
        ensureDirectory("/foo/bar"); // Make sure you have proper access to the give path
    }

    public static void ensureDirectory(String dirPath) {
        if (dirPath != null && !dirPath.isEmpty()) {
            File dir = new File(dirPath);
            if (!dir.exists()) {
                Path newDirPath = Paths.get(dirPath);
                if (!Files.exists(newDirPath)) {
                    try {
                        Files.createDirectories(newDirPath);
                    } catch (IOException e) {
                        throw new RuntimeException("Exception while creating directory: " + dirPath, e);
                    }
                }
            }
        }
    }
}

If you have proper permissions to the given path, the utility method will create the directory structure for you of not exists. If you do not have proper permissions, you may get following exception

Exception in thread "main" java.lang.RuntimeException: Exception while creating directory: /foo/bar
    at com.Test.ensureDirectory(Test.java:24)
    at com.Test.main(Test.java:12)
Caused by: java.nio.file.AccessDeniedException: /foo
    at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
    at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:384)
    at java.nio.file.Files.createDirectory(Files.java:674)
    at java.nio.file.Files.createAndCheckIsDirectory(Files.java:781)
    at java.nio.file.Files.createDirectories(Files.java:767)
    at com.Test.ensureDirectory(Test.java:22)
    ... 1 more
On By


Answer This Question


Top Tutorials

Top Questions

Top Articles

Top Blogs

Top News