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
Joined: Aug 27, 2004
Posts: 32
posted
0
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
Joined: Feb 23, 2005
Posts: 33
posted
0
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
Joined: Jan 26, 2005
Posts: 76
posted
0
Thanks a lot..I will try the solutions provided.
ford Darcy Jr
Ranch Hand
Joined: Jan 26, 2005
Posts: 76
posted
0
Thanks a lot...I was able to get the filename of the uploaded file.