71. simplify path:
简化ubuntu中的文件路径
当遇到"/../"时,需要返回上级目录,返回之间应该检查上级是否为空,若为空的话,则就代表"/"
当遇到"//"或"/./"时,不用做任何操作,这都代表的是本级目录;
遇到其它字符时,则代表的是文件夹名;
*代码处理时:字符串间的比较采用equals()方法,使用"=="比较出错.
public String simplifyPath(String path) {
Stack<String> st = new Stack<String>();
String result="";
String[] str = path.split("/");
for(String temp:str){
if(temp.equals(".")||temp.length()==0) ; //均表示本级目录
else if(temp.equals("..")){
if(st.isEmpty()); //上级目录为空的情况
else st.pop();
}
else
st.push(temp);
}
if(st.isEmpty())
return "/";
else{
while(!st.isEmpty()){
result="/"+st.pop()+result;
}
}
return result;
}