Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

String

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks a lot...I was able to get the filename of the uploaded file.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic