Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Extract Filename

Thread view: 
Noodle - 05 Oct 2006 12:22 GMT
Hi There,

Can anyone tell me what is the best way to seperate a filename and a
path so I'm left with 2 strings.

e.g.
Turn this input: "C:\path\to\my\file.txt" into:
String path = "C:\path\to\my";
String file   = "file.txt";

as well as turning this: "/path/to/my/file.txt"
String path = "/path/to/my";
String file   = "file.txt";

TIA
Gordon Beaton - 05 Oct 2006 12:28 GMT
> Can anyone tell me what is the best way to seperate a filename and a
> path so I'm left with 2 strings.

Intuitively I would use methods in java.lang.String to find the last
instance of the separator character, then create substrings around
that point.

However there are also methods in java.io.File that will do exactly
this for you: getName() and getParent().

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

Tim Slattery - 05 Oct 2006 17:26 GMT
>Hi There,
>
[quoted text clipped - 9 lines]
>String path = "/path/to/my";
>String file   = "file.txt";

String fullPath = new String("/path/to/my/file.txt");
int i = fullPath.lastIndexOf("/");
String path = fullPath.substring(0, i);
String file = fullPath.substring(i + 1);

--  
Tim Slattery
Slattery_T@bls.gov
Steve W. Jackson - 05 Oct 2006 18:28 GMT
> >Hi There,
> >
[quoted text clipped - 18 lines]
> Tim Slattery
> Slattery_T@bls.gov

As was pointed out in an earlier reply, there's another way that IMHO is
most definitely better than anything involving lastIndexOf since the OP
specifically shows x-plat interest.  That's the use of the java.io.File
class instead.
Signature

Steve W. Jackson
Montgomery, Alabama

Noodle - 06 Oct 2006 01:25 GMT
> >Hi There,
> >
[quoted text clipped - 18 lines]
> Tim Slattery
> Slattery_T@bls.gov

Thank you everyone for your speedy reply's. I've based my code on Tim's
reply and it works well...

String fullPath = "/path/to/my/file.txt";
int i = fullPath.lastIndexOf(System.getProperty("file.separator"));
String path = fullPath.substring(0, i);
String file = fullPath.substring(i + 1);
Baby Lion - 05 Oct 2006 17:29 GMT
do you mean by  using   String::lastIndexOf("/");
Noodle 写道:

> Hi There,
>
[quoted text clipped - 11 lines]
>
> TIA


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.