• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am uploading a file in servlets. Now..I just need the filename from the filepath which I am uploading. I mean...is there any way to get just the filename (or say the last set of characters) from the filepath. I need to use this filename elsewhere...I cant hardcode it.

Thanks.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what you can do to get the filename out of filepath is, treat filepath as string and get the index of last occurence of "/" in the filepath and substring the filename from that index value till the end of the filepath.

For example:

String filepath = "c:/documents and setting/desktop/cdss/file1.txt"

Here in this case filename is file1.txt. To get this what we can do is

int idx = filepath.lastIndexOf("/");
String filename = filepath.substring(idx+1);

This will give "file1.txt" in filename.

Hope this helps.

Anna
[ March 03, 2005: Message edited by: anna pillutla ]
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ford Darcy Jr:

Now..I just need the filename from the filepath which I am uploading. I mean...is there any way to get just the filename (or say the last set of characters) from the filepath.



The java.io.File class has a getName() method you can use if you have a File object. I believe you could create a File object and get the name for it as follows:

java.io.File myFile = new File("C:/dir1/dir2/dir3/myFileName.txt");
String myFileName = myFile.getName();

I'm pretty sure that's it, but you'll want to look at the javadocs for java.io.File and try it out.
 
ford Darcy Jr
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot..I will try the solutions provided.
 
ford Darcy Jr
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot...I was able to get the filename of the uploaded file.
 
Good night. Drive safely. Here's a tiny ad for the road:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic