Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mkDir(s) vs Files.createDirectory(ies)

Tags:

java

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.

like image 982
Oleksii Karnatskyi Avatar asked Mar 12 '18 12:03

Oleksii Karnatskyi


People also ask

What is difference between mkdir and Mkdirs?

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 .

What is mkdir () in Java?

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()

What is mkdir file?

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.

Does mkdir overwrite existing directory in Java?

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.


1 Answers

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
like image 82
htshame Avatar answered Sep 29 '22 01:09

htshame