Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 324
M.J. Avatar asked Oct 02 '11 14:10

M.J.


People also ask

How can you tell if a path is relative or absolute?

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 is relative path and absolute path in Java?

A path can be absolute or relative. An absolute path contains the full path from the root of the file system down to the file or directory it points to. A relative path contains the path to the file or directory relative to some other path.


1 Answers

How about File.isAbsolute():

File file = new File(path); if (file.isAbsolute()) {     ... } 
like image 200
Jon Skeet Avatar answered Sep 26 '22 06:09

Jon Skeet