Demo:
import java.nio.file.Path; import java.nio.file.Paths; /** * @author jinxing * @系统 MAC OS X * @用例1 [使用]绝对路径 * @用例2 [使用]相对路径 * @用例3 相对路径[转换成]绝对路径 * @结论1 [使用]相对路径,相对的是[..当前文件所属项目文件/下文件]; * @结论2 最后不论加否[/反斜杠]结果都一样;(不如不加) */ public class PathTest { // Path或Paths中的各种方法抛出的受检异常只有IOExcetion public static void main(String[] args) { // [使用]绝对路径 Path useabs = Paths.get("/Users/jinxing/Documents/pathtest"); Path useabsz = Paths.get("/Users/jinxing/Documents/pathtest/"); // [使用]相对路径 Path userel = Paths.get(".."); Path userelz = Paths.get("../"); // 相对路径[转换成]绝对路径 Path relToAbs = userel.toAbsolutePath(); Path relToAbsz = userelz.toAbsolutePath(); // show System.out.println(useabs.toString()); System.out.println(useabsz.toString()); System.out.println(userel.toString()); System.out.println(userelz.toString()); System.out.println(relToAbs.toString()); System.out.println(relToAbsz.toString()); } }
Ran As Java Application:
/Users/jinxing/Documents/pathtest /Users/jinxing/Documents/pathtest .. .. /Users/jinxing/Documents/workspace/nio.2/.. /Users/jinxing/Documents/workspace/nio.2/..