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 / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

appending paths

Thread view: 
Rex Guo - 29 Mar 2004 16:44 GMT
Hi!

Given 2 paths as File objects:
1. C:/a/b/c/
2. ../d

How do I append #1 and #2 to get "C:/a/b/d"?

Are there any standard Java API that does this?
Please don't suggest string manipulation! ;-)
Thanks!

.rex
Alexander Mueller - 29 Mar 2004 16:48 GMT
> Hi!
>
[quoted text clipped - 9 lines]
>
> .rex

new File("/a/b/c/../d").getCanonicalPath()

  will return the shortened path.

Alexander
Signature

LocalHost Creations
http://www.localhost.st

Jon A. Cruz - 29 Mar 2004 18:33 GMT
>> Hi!
>>
[quoted text clipped - 13 lines]
>
>   will return the shortened path.

Yes... but that's not quite the best way.
Alexander Mueller - 30 Mar 2004 13:05 GMT
> Yes... but that's not quite the best way.

Well, it was an example pointing out the getCanonicalPath() method. What
would you consider as better way?

Alexander
Signature

LocalHost Creations
http://www.localhost.st

Andrew Thompson - 30 Mar 2004 13:56 GMT
>> Yes... but that's not quite the best way.
...
> Well, it was an example pointing out the getCanonicalPath() method.

Examples are good, especially
since they can produce
a) confirmation that it is the best way, ..or

>..What
> would you consider as better way?

b) ..something better.

Like, probably this.      ;-)
<http://groups.google.com/groups?th=b95d40398ed18a81#link6>

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Alexander Mueller - 30 Mar 2004 14:06 GMT
> Like, probably this.      ;-)
> <http://groups.google.com/groups?th=b95d40398ed18a81#link6>

It fully depends on the context where the code is used. This example
creates two (or more) objects with both requiring time and memory which
might not be necessary in this case.

Alexander
Signature

LocalHost Creations
http://www.localhost.st

John C. Bollinger - 29 Mar 2004 17:36 GMT
> Hi!
>
[quoted text clipped - 7 lines]
> Please don't suggest string manipulation! ;-)
> Thanks!

Look at the API docs for the File(File, String) constructor.  There will
 likely be problems if the second spec is an absolute pathname (in that
the Java platform's system-dependent choice of behavior in that case may
not be the behavior you want).

Once you have a File representing the combination, there are several
ways to get the pathname string out of it.

John Bollinger
jobollin@indiana.edu
Jon A. Cruz - 29 Mar 2004 18:40 GMT
> Hi!
>
[quoted text clipped - 7 lines]
> Please don't suggest string manipulation! ;-)
> Thanks!

File rootDir = new File( "C:" );
File baseDir = new File( rootDir, "a/b/c" );
...

File relative = new File( baseDir, "../d" );
File wanted = relative.getCanonicalFile();

However... if you're doing the path building, it's best to avoid
hardcoding slashes, or even file.separator. Instead just use file
constructors.

"../d"
becomes
new File( "..", "d" );

"a/b/d
becomes
new File( "a", new File( "b", "c") );

In actuall code you usually won't have constructors quite like that,
since you'll be using File variables, and it protects you from subtle
gotchas of different platforms (like how on Windows starting with a
double file.separator will turn it from a file path into a UNC network
path).
Rex Guo - 30 Mar 2004 02:46 GMT
Cool! A big Thank You to all who answered!

.rex


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.