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