0

createNewFile():

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file.

How does it know if a file can be created or not ? Does it depend on the host platform ?

Bionix1441
  • 2,135
  • 1
  • 30
  • 65
  • 1
    Evidently host platform API used (because of the atomicity). – Joop Eggen Mar 25 '15 at 09:55
  • 2
    In 2015 you don't want to use that anyway; use java.nio.file and `Files.createFile()`. At least it will throw a meaningful exception if it fails, unlike `File`'s `.createNewFile()`. – fge Mar 25 '15 at 10:03

1 Answers1

2

You can take a look at the OpenJDK repository. What you are looking for is this. By looking into it, you can find, that this method calls FileSystem.createFileExclusively(java.lang.String), where FileSystem is an abstract class and the concrete instance is obtained via FileSystem.getFileSystem() which is native method, specific for different platforms.

EDIT Example for Win32 File System is here

nikis
  • 11,166
  • 2
  • 35
  • 45