my dog learned polymorphism
The moose likes Beginning Java and the fly likes basename of a file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "basename of a file?" Watch "basename of a file?" New topic

basename of a file?

Sheldon Plankton
Ranch Hand

Joined: Feb 11, 2004
Posts: 33
I what to be able to get the "basename" of a String that maybe be a file's name. I know how to do this in a shell script which I'll will post as demostration of what I want to be able to do in Java ...
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
The File object is used to manipulate files and directories. Use getName() to get the filename in the format you want.
The code below doesn't do exactly what you want (I figure you can do some work yourself , but it should give you examples of the classes and methods you could use.

--Tim West
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Lemme see if I hear your problem ... you have a string which might contain d:\path\name.ext and you want to extract just name.ext? Look into lastIndexOf() to find the last slash, and then substring to get the name.ext that follows. Be sure to try it on a string with no slashes at all. Maybe do all this once for forward slash / and again for back slash \.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: basename of a file?
It's not a secret anymore!