Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

basename of a file?

 
Sheldon Plankton
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Cheers,

--Tim West
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 \.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic