Java's way of distinguishing between absolute and relative paths
- 2020-04-01 03:48:30
- OfStack
This article illustrates how Java distinguishes between absolute and relative paths. Share with you for your reference. Specific analysis is as follows:
The difference here is the directory path
Such as:
/ opt/deve/tomcat/bin
C: \ deve \ tomcat \ bin
Both are absolute directory paths
bin
Bin/data
Bin \ data
Relative directory paths
Through observation, discover the rule
Everything that starts with/or contains \ or // is an absolute path or
Everything that starts with/or contains: is an absolute path
The opposite is the relative path
Introduce several methods:
startsWith
public class Stringutil {
public static void main(String[] args) {
String path = "/opt/bin";
System.out.println(path.startsWith("/"));
}
}
Results: true,
indexOf
Final results:
public boolean isAbsolutePath(String path) {
if (path.startsWith("/") || path.indexOf(":") > 0) {
return true;
}
return false;
}
I hope this article has been helpful to your Java programming.