Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How to find out if path is absolute regardless of the OS

Is there anyway in Java to find out if the given path is absolute or not regardless of the platform the program is currently running. So, what I want is probably something like the following example:

On Linux:

new File("/home/").isAbsolute() // Should return true.
new File("C:/My Documents").isAbsolute() // Should *also* return true.

On Windows:

new File("C:/Documents").isAbsolute() // Should return true.
new File("/home/").isAbsolute() // Should *also* return true.

I can probably code something to get around with this, but I just wanted to find out if anyone knew a built-in class provided in Java to solve this problem. Or has anyone ever come this problem? And how did you solve it?

Thanks!

like image 402
His Avatar asked Jun 22 '09 02:06

His


People also ask

How do you tell if a path is an absolute path?

A path starting with a drive letter followed by a colon not followed by a backslash (or slash) can be relative to that drive's current directory. A path starting with a backslash (or slash) is absolute but on the current drive, so in that sense it is in fact relative (to the top of the current drive).

How do I check if Java path is correct?

Open a Command Prompt window (Win⊞ + R, type cmd, hit Enter). Enter the command echo %JAVA_HOME% . This should output the path to your Java installation folder. If it doesn't, your JAVA_HOME variable was not set correctly.

What is the difference between absolute path and relative path name in OS?

An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path is defined as the path related to the present working directly(pwd).

What does an absolute path Start with your operating system?

An absolute path refers to the complete details needed to locate a file or folder, starting from the root element and ending with the other subdirectories. Absolute paths are used in websites and operating systems for locating files and folders. An absolute path is also known as an absolute pathname or full path.


2 Answers

Nope.

There are some underlying FileSystem classes (that's Java 7, but they exist prior to it as well) that expose isAbsolute(), but they're not public - so you shouldn't use them, and even if you did your code would be full of reflection junk - and only the "correct" OS ones are included in the JRE, so you'd have to code around them anyway.

Here are the Java 7 implementations of isAbsolute(...) to get you started. Note that File.getPrefixLength() is package-private.

Win32FileSystem:

public boolean isAbsolute(File f) 
{
        int pl = f.getPrefixLength();
        return (((pl == 2) && (f.getPath().charAt(0) == slash))
                || (pl == 3));
}

UnixFileSystem:

public boolean isAbsolute(File f) 
{
        return (f.getPrefixLength() != 0);
}
like image 146
Kevin Montrose Avatar answered Nov 04 '22 16:11

Kevin Montrose


In Java 7:

new File(path).isAbsolute()
like image 22
Val Blant Avatar answered Nov 04 '22 15:11

Val Blant