Demo:import java.nio.file.Path;import java.nio.file.Paths;/** * @author jinxing * @系統 MAC OS X * @用例1 [使用]絕對路徑 * @用例2 [使用]相對路徑 * @用例3 相對路徑[轉換成]絕對路徑 * ...
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/..