This Q is from RHE. You execute the code below in an empty directory. What is the result.
The answer given is, A new directory called dirname is created in the current working directroy. A new file called filename is created in directory dirname. But I selected the answer, no directory or file created. Why my answer is wrong?
Hi, Thiru The constructor of File class doesn't create a file - it just creates an instance... If you want to create a new file, you must call createNewFile() method: File f = new File("test.txt"); f.createNewFile(); Quote from JDK API, File class
public File(String pathname)Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname. public boolean createNewFile() throws IOExceptionAtomically 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. This method, in combination with the deleteOnExit() method, can therefore serve as the basis for a simple but reliable cooperative file-locking protocol. Returns: true if the named file does not exist and was successfully created; false if the named file already exists