• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

checking for file existence

 
Steve Matthews
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the double slash is always needed. '\' is an escape character in java.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
M.K.A. Monster
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, or System.getProperty("file.separator");
[ May 02, 2002: Message edited by: Dirk Schreckmann ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, what Jim said. I had misread what Monster "if-that-is-your-real-name" was saying.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic