How to create a Java directory using NIO.2 - Java SE 7

Asked

Viewed 964 times

4

The API java. is part of the Java SE platform since its version 1.4. Now, from Java 7, we have several new Enhancements in this API and new features, which provide better management of the file system (file system) and offer various utility methods.

How we create one directory using such resources?

How we create directories using such resources?

What are the particularities of the use of these methods?

1 answer

3


The class java.nio.files.Files brings with it much of these new features and facilities.

In case of the need to create one or more directories, let’s see how the static methods Files.createDirectory(Path) and Files.createDirectories(Path) work.

How we create one directory using such resources?

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

public class Main {
    public static void main(String[] args) {
        Path path = Paths
                .get("/home/user/Documents/directoryCreatedFromJavaApp2");
        try {
            Files.createDirectory(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In the class above, we are first using the class Path, representing the path - path - to the directory file we want to create. We get an instance of it from the call to the static manufacturing method that is in the class Paths. We define through the method Paths.get(String) - through the String passed as parameter, which is the directory we want to create.

...createDirectory(Path) - in the singular

Exactly, here lies an important detail. This method creates only one directory, the last one in our String: /home/user/Documents/directoryCreatedFromJavaApp2

That is, we create here the directory directoryCreatedFromJavaApp2.

So the directories before this must already exist?

Exactly, if they do not exist, the method will launch java.nio.file.NoSuchFileException indicating the absence of the previous path, which leads us to have to capture IOException (also we can only propagate the exception depending on our design).

And if the directory I’m creating already exists?

In this case the method Files.createDirectory(Path) will make an exception: java.nio.file.FileAlreadyExistsException, so the need to continue capturing IOException - superclass of FileAlreadyExistsException.

How we create directories using such resources?

So now we’ll see the method Files.createDirectories(Path).

Let’s take an example:

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

public class Main {
    public static void main(String[] args) {
        Path path = Paths
                .get("/home/user/Documents/directory1/directory2/directory3");
        try {
            Files.createDirectories(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Again, we noticed in the code above that we had to use an object Path. In fact the only difference was in the method for creating the directory, in our case, ...Directories plural. Now an important point, unlike the previous method, this method does not require that the parent directories of the last directory exist, because if they do not exist, they will be created.

And if the directory already exists when calling Files.createDirectories(Path)?

No exception will be thrown, in fact, nothing will happen with the directory

For more details see the Javadocs: http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.