M.J.
M.J.

Reputation: 16646

How to check whether the path is relative or absolute in java?

I am developing a tool, which takes a path of an xml file. Now that path can be either relative or absolute. Inside the code, when I have only a string, is there a way to identify that the path is absolute or relative?

Upvotes: 43

Views: 32643

Answers (2)

El Marce
El Marce

Reputation: 3344

There is another very similar way using Paths operations:

Path p = Paths.get(pathName); 
if (p.isAbsolute()) {
    ...
}

Upvotes: 29

Jon Skeet
Jon Skeet

Reputation: 1500675

How about File.isAbsolute():

File file = new File(path);
if (file.isAbsolute()) {
    ...
}

Upvotes: 68

Related Questions