简体   繁体   中英

How does createNewFile internally works?

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM