Re: A "current directory" concept for Java...
Andreas Leitgeb schrieb:
e.g.: the wrapped Path of curDir originally points to "/tmp", then...
- curDir.chDir("foo") would go to "/tmp/foo", then
- curDir.chDir("../bar") would go to "/tmp/bar", then
- curDir.chDir("/foo") would go to "/foo"
if you use a static variable to hold the current
directory, it will be seen class loader wide.
Means for a standalone application you will
have one current directory, and for web applications
it could happen that each web context has its own
current directory.
public class State {
private static String curdir;
public void chDir(String str) {
curdir = new FIle(curdir,str).getCanonicalPath();
}
}
The getCanonicalPath() method will usually resolve
.../ and ./ parts in the str together with curdir.
But I am not sure how binding the Java contract is.
I find in the comment only saying "typical":
"A canonical pathname is both absolute and unique.
The precise definition of canonical form is system-
dependent. This method first converts this pathname
to absolute form if necessary, as if by invoking the
{@link #getAbsolutePath} method, and then maps it to
its unique form in a system-dependent way. This
typically involves removing redundant names such as
<tt>"."</tt> and <tt>".."</tt> from the pathname,
resolving symbolic links (on UNIX platforms), and
converting drive letters to a standard case (on
Microsoft Windows platforms)."
Bye