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 Scanner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Scanner" Watch "Scanner" New topic


dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383

It is given that the file(a.txt) is already exist. Why we need new File(a.txt) in constructor parameter. Can anyone please explain?

Scanner sc=new Scanner (new File(a.txt));

SCJP-1.5<br />SCWCD-1.4
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41034
Because "new File(...)" does not create a file - it creates a File object. You need a File object to work with existing files as well as new files.

Ping & DNS - my free Android networking tools app
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Thanks Ulf.
It is sorta covered in the JavaRanch Style Guide.
subject: Scanner
Similar Threads
Best data storage method
file creation
Multitread synchronization
File Reading using J2ME