What is the difference between file.mkDir()
and Files.createDirectory(path)
.
Is there any cases which suites better for any of them? Or Files.createDirectory(path)
is just newer version of file.mkDir()
(from newer Java version?
Thanks in advance.
mkdirs() will create the specified directory path in its entirety where mkdir() will only create the bottom most directory, failing if it can't find the parent directory of the directory it is trying to create. In other words mkdir() is like mkdir and mkdirs() is like mkdir -p .
The mkdir() method is a part of File class. The mkdir() function is used to create a new directory denoted by the abstract pathname. The function returns true if directory is created else returns false. Function Signature: public boolean mkdir() Syntax: file.mkdir()
The mkdir() function creates a new, empty directory whose name is defined by path. The file permission bits in mode are modified by the file creation mask of the job and then used to set the file permission bits of the directory being created.
mkdir -p WILL NOT give you an error if the directory already exists. Also, the directory will remain untouched i.e. the contents are preserved as they were.
The difference is described in java docs.
file.mkDir()
:
- Creates the directory named by this abstract pathname.
- @return
true
if and only if the directory was created;false
otherwise
Files.createDirectory(path)
:
- Creates a new directory. The check for the existence of the file and the creation of the directory if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the directory. The {@link #createDirectories createDirectories} method should be used where it is required to create all nonexistent parent directories first.
The {@code attrs} parameter is optional {@link FileAttribute file-attributes} to set atomically when creating the directory. Each attribute is identified by its {@link FileAttribute#name name}. If more than one attribute of the same name is included in the array then all but the last occurrence is ignored.
- @return the directory
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With