aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes IO Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "IO" Watch "IO" New topic
Author

IO

Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
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?


Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
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

Regards,
Jamal Hasanov
www.j-think.com
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
Thanks Jamal,
So, I can say the answer I selected is correct.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IO
 
Similar Threads
problrm in creation of file and dircectory
file Deletion
How to set folder permissoins
Folder traverse from a location
why it throws NullPointerException