如:
/a/b/c/ 变成 /a/b/c
/a//b/ 变成 /a/b
/a/./…/b/…/c/ 变成 /c
public static void main(String[] args) {
System.out.println("整理之后的目录结构是:" + packageDir("/a/b/c/"));
System.out.println("整理之后的目录结构是:" + packageDir("/a//b/"));
System.out.println("整理之后的目录结构是:" + packageDir("/a/./../b/../c/"));
}
public static String packageDir(String path){
String[] strs = path.split("/");
Deque<String> stack = new LinkedList<>();
for (String str:strs){
if (str.equals("..")){
if (!stack.isEmpty()){
stack.pop();
}
}else if(!str.isEmpty() && !str.equals(".")){
stack.push(str);
}
}
String res = "";
for (String item:stack){
res = "/" + item + res;
}
return res.isEmpty()?"/":res;
}