my dog learned polymorphism
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "checking for file existence" Watch "checking for file existence" New topic

checking for file existence

Steve Matthews

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:-
and not like this:-
Is the double slash always required ?
Is there an easier way,
Bosun Bello
Ranch Hand

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

So much trouble in the world -- Bob Marley
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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.
[ April 30, 2002: Message edited by: Dave Vick ]

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

Joined: Jan 10, 2002
Posts: 63838

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".

[Asking smart questions] [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

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

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

Joined: Dec 10, 2001
Posts: 7023
Yeah, what Jim said. I had misread what Monster "if-that-is-your-real-name" was saying.
I agree. Here's the link:
subject: checking for file existence
It's not a secret anymore!