File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "File" Watch "File" New topic


siddharth das
Ranch Hand

Joined: Aug 17, 2007
Posts: 131
what is the difference between
File f = new File("a,txt");
where a.txt file created?
Ankit Garg

Joined: Aug 03, 2008
Posts: 9465

Errata: Change

File f = new File("a,txt");


File f = new File("a.txt");

. After that when you write that statement, the object will start referring to a PATH which has a.txt in the end. It is not necessary that the file a.txt is present. But when you call


then if a.txt doesn't exists, it will be created. So you can be sure that after call to createNewFile, the object f refers to a file a.txt...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ofcourse when f.createNewFile() is called then.

File is just a class, if you call new File("filename"), it just creates an object of file class. But if you want to create filename file in the system, you have to call File.createNewFile().

Any doubt???

siddharth das
Ranch Hand

Joined: Aug 17, 2007
Posts: 131
what is the meaning of
File file = new File(directory,�f�);
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

Lookup class File in the API documentation.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Pranav Patel

Joined: Dec 11, 2008
Posts: 6
It will create a instance of file named �f� at the �directory� Directory on the heap.
To persist it, first you need to check - either directory folder should be there OR you need to call directory.mkdir(). And then you need to call file.createNewFile().
I agree. Here's the link:
subject: File
It's not a secret anymore!