File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String

 
ford Darcy Jr
Ranch Hand
Posts: 76
  • 0
  • 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.
 
anna pillutla
Ranch Hand
Posts: 32
  • 0
  • 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 ]
 
H Wilson
Ranch Hand
Posts: 33
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic