For Programmers: Free Programming Magazines  


Home > Archive > Java Help > March 2004 > appending paths









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author appending paths
Rex Guo

2004-03-29, 11:51 am

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

2004-03-29, 11:51 am

Rex Guo wrote:

> 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


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

will return the shortened path.

Alexander
--
LocalHost Creations
http://www.localhost.st
John C. Bollinger

2004-03-29, 12:35 pm

Rex Guo wrote:

> 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!


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

2004-03-29, 1:56 pm

Alexander Mueller wrote:
> Rex Guo wrote:
>
>
>
> new File("/a/b/c/../d").getCanonicalPath()
>
> will return the shortened path.
>


Yes... but that's not quite the best way.

Jon A. Cruz

2004-03-29, 1:56 pm

Rex Guo wrote:
> 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!


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

2004-03-29, 9:39 pm

Cool! A big Thank You to all who answered!

..rex
Alexander Mueller

2004-03-30, 7:40 am

Jon A. Cruz wrote:
>
> 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
--
LocalHost Creations
http://www.localhost.st
Andrew Thompson

2004-03-30, 8:38 am

On Tue, 30 Mar 2004 14:05:52 +0200, Alexander Mueller wrote:

> Jon A. Cruz wrote:
....[color=darkred]
> 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=...98ed18a81#link6>

--
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

2004-03-30, 8:38 am

Andrew Thompson wrote:
>
> Like, probably this. ;-)
> <http://groups.google.com/groups?th=...98ed18a81#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
--
LocalHost Creations
http://www.localhost.st
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com