The example in this article describes how Java distinguishes between absolute paths and relative paths. Share it with everyone for your reference. The specific analysis is as follows:
What needs to be distinguished here is the directory path
like:
/opt/deve/tomcat/bin
c:/deve/tomcat/bin
All are absolute directory paths
bin
bin/data
bin/data
All are relative directory paths
Discover patterns through observation
Anything that starts with / or contains / or // is an absolute path; anything that starts with / or contains : is an absolute path, otherwise it is a relative path.
Introducing several methods:
startsWithpublic class Stringutil { public static void main(String[] args) { String path = "/opt/bin"; System.out.println(path.startsWith("/")); }}
Result: true
indexOf
Final result:
/** * Pass in the path and return whether it is an absolute path. If it is an absolute path, it returns true, otherwise * * @param path * @return * @since April 21, 2015 */public boolean isAbsolutePath(String path) { if ( path.startsWith("/") || path.indexOf(":") > 0) { return true; } return false;}
I hope this article will be helpful to everyone’s Java programming.