File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Get Filename using substring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Get Filename using substring" Watch "Get Filename using substring" New topic
Author

Get Filename using substring

nandhini sruthi
Greenhorn

Joined: Jan 11, 2008
Posts: 17
Hi,
I need help for get only the filename from attachment url D:\MDM\get_url.txt it is not default.this url is change everytime attachment.please help me how to get filename without txt.

thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Sounds more like a beginner's question.
You can parse the String with the methods of the String class and find the index of the "." and then get a substring.
[ July 23, 2008: Message edited by: Campbell Ritchie ]
nandhini sruthi
Greenhorn

Joined: Jan 11, 2008
Posts: 17
Hi,
I tried using substring.but I got some problem in that. Please give the solution. I put my code here.



If i use the like above(ie.I pass the value directly)i got exact filename get_url.but i can't use like this.When I click the browse button I take any file and its path may be different. I definitely use like follows. But that time I can't get exact filename.


If i use the like above I got filename D:\MDM\get_url.please give the solution.

Thanks in advance.
Niki Nono
Ranch Hand

Joined: Mar 20, 2005
Posts: 256
Check for file path seperator. In your case, it seems to be the '\' and not the '/' which you are checking.
1. Check what the file path seperator is and store it in a variable.
2. Use this variable to get to the last occurence of the seperator.


Life called,so here I am.<br />Cheers<br />Niki.:-)
vedha vas
Greenhorn

Joined: Jul 22, 2008
Posts: 10
Hi Nandhini Sruthi ,

Try this way,

String url = "c\\documentsandsettings\\user\\privatefiles\\get_url.txt";
int dotCnt = url.indexOf(".");
String str = url.substring(0, dotCnt);
int slashCnt = str.lastIndexOf("\\");
String filename = str.substring(slashCnt+1);
System.out.println(filename);


Regrads,<br />Vedha.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Niki Nono was nearly correct; you look for the path separator and file separator. But you don't have to search far for them; there are fields in the File class called separator or pathSeparator which encapsulate that information, so you can find the last index of those characters. You can also find them from the System class.
Look through the File class and see which you need; I think it's "separator," not "path separator."
No need to hard-code "\\" or "/" which would limit portability.

Try

. . . myURL.substring(myURL.lastIndexOf(File.separator) + 1, myURL.lastIndexOf(".")); or something similar.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
My technique will not cause problems if you pass a String like "myFile.txt" without / or \\ but you will get problems if there is no "." in it anywhere. That might require two substring calls.
nandhini sruthi
Greenhorn

Joined: Jan 11, 2008
Posts: 17
Hi,
Thanks for reply.I got the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get Filename using substring