This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes checking for file existence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "checking for file existence" Watch "checking for file existence" New topic
Author

checking for file existence

Steve Matthews
Greenhorn

Joined: Apr 18, 2002
Posts: 6
Dear All,
Thankyou for your most useful responses to date.
I have another question:
I would like to know how to determine whether a file EXISTS in a specified folderpath. I have attempted to do it by writing this function:-
private boolean check_file(String path, String fname)
{
boolean status = false;
File dir = new File(path);
File fil = new File(dir, fname);
status = fil.exists();
return status;
}
Trouble is, this only works if 'path' contains a string like this:-
c:\\dir1\\dir2\\dir3
and not like this:-
c:\dir1\dir2\dir3
Is the double slash always required ?
Is there an easier way,
Thanks,
Steve
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Yes, the double slash is always needed. '\' is an escape character in java.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Steve
Thanks for changing your name like Dirk asked you to. However, did you actually read the naming policy? You must not have because we ask that you use two names - a first and a last with a space in the middle. You can change you name here.
Thanks
[ April 30, 2002: Message edited by: Dave Vick ]

Dave
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
also you can use a forward slash instead of 2 back ones.
so you can use this / instead of \\
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

A little more on escaping:
In Java string literals, the back-slash character is used to create special characters that would otherwise not be able to be embedded within the literal. Some examples are: \t for tab, \n for new-line, \r for carriage return and so on. Because the back-slash is used in this way, in order to include a 'real' back-slash in the literal you must use \\ which evaluates to a single back-slash in the literal.
Thus when a Java string literal is written as "c:\\this\\that\\theother", the text that is actually stored in the resulting String object is "c:\this\that\theother".
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
M.K.A. Monster
Ranch Hand

Joined: May 02, 2002
Posts: 130
Originally posted by Bosun Bello:
Yes, the double slash is always needed. '\' is an escape character in java.

The best way is to use, File.seperatorChar; So this will work in every platform.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Sure, or System.getProperty("file.separator");
[ May 02, 2002: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, you can certainly use System.getProperty("file.separator") - but File.separatorChar or File.separator is more efficient, as it doesn't require a Hashtable lookup using a String key for each access. Plus it's slightly less typing.


"I'm not back." - Bill Harding, Twister
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Yeah, what Jim said. I had misread what Monster "if-that-is-your-real-name" was saying.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: checking for file existence