Bruce
Bruce

Reputation: 3528

How to get the absolute path of a file when given a relative or absolute path and the absolute path it is relative to

Let's say I have an absolute 'base' path:

/home/someone/dir1/dir2/

The user can pass me a new path, that can either be absolute or relative to base path, so the following would both be valid:

..
/home/someone/dir1/

How do I get java to give me the correct absolute path ie for both these cases:

/home/someone/dir1/

and do this in a platform-independent way?

I tried the following:

File resolvedFile = new File((new File(basePath).toURI().resolve(new File(newPath).toURI())));

However, where newPath was relative, newFile(newPath) resolves it automatically against the current working directory, rather than the basePath I want to supply.

Any thoughts? Many thanks!

Upvotes: 1

Views: 3519

Answers (4)

apierion
apierion

Reputation: 1

Not sure if this works outside of my setup (windows platform, JRE 1.6.x)

but the following worked like a trick:

File path = new File(relativeOrAbsoluteGoldpath);
absolutePath = path.getCanonicalPath();

where relativeOrAbsoluteGoldpath is an arbitrary path name that may or may not be relative.

Upvotes: 0

Abhinaba Basu
Abhinaba Basu

Reputation: 351

try this in your code.

System.setProperty("user.dir", "your_base_path")

Upvotes: 0

Bruce
Bruce

Reputation: 3528

Answering my own question..

Seems like it can be done in java 7 using Path:

Path p1 = Paths.get("/home/joe/foo");
// Result is /home/joe/foo/bar
System.out.format("%s%n", p1.resolve("bar"));

Since I can't get java 7 for my mac 10.5.8, I'm going with something like (NB NOT THOROUGHLY TESTED!):

static String getAbsolutePath(String basePath, String relativeOrAbsolutePath) throws IOException {

    boolean isAbsolute = false;
    File relativeOrAbsoluteFile = new File(relativeOrAbsolutePath);

    if (relativeOrAbsoluteFile.isAbsolute()){
        isAbsolute = true;
    }

    if (isAbsolute){
        return relativeOrAbsolutePath;
    }
    else {
        File absoluteFile = new File(basePath, relativeOrAbsolutePath);
        return absoluteFile.toString();
    }

}

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347194

Take a look at File#getCanonicalPath

From the JavaDocs:

Returns the canonical pathname string of this abstract pathname. 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 getAbsolutePath() method, and then maps it to its unique form in a system-dependent way. This typically involves removing redundant names such as "." and ".." from the pathname, resolving symbolic links (on UNIX platforms), and converting drive letters to a standard case (on Microsoft Windows platforms).

Every pathname that denotes an existing file or directory has a unique canonical form. Every pathname that denotes a nonexistent file or directory also has a unique canonical form. The canonical form of the pathname of a nonexistent file or directory may be different from the canonical form of the same pathname after the file or directory is created. Similarly, the canonical form of the pathname of an existing file or directory may be different from the canonical form of the same pathname after the file or directory is deleted.

Upvotes: 0

Related Questions