如何在Java中模仿cd命令

时间:2015-03-27 01:37:54

标签: java string algorithm cd

给你两条路径,一条是原始路径,另一条是cd命令的参数。

例如:

users/documentsstackOverFlow应该返回users/documents/stackOverFlow

users/documents../stackOverFlow应该返回users/stackOverFlow

users/documents/test../../stackOverFlow应该返回users/stackOverFlow

我们如何实施这样的方法 imitateCDCommand有两个参数str1str2并返回目标路径。

1 个答案:

答案 0 :(得分:0)

import java.io.File;
import java.io.IOException;
// ...
public static String imitateCDCommand(dir1, dir2) throws IOException {
  File file1 = new File(dir1);
  File file2 = new File(file1, dir2);
  return file2.getCanonicalPath();
}