Path is not system independent.You cannot compare a
Path from a Solaris file system and expect it to match a Path from a Windows file system, even if the directory structure is identical and both instances locate the same relative file. Path p1 = Paths.get(new URI("file://e:/temp/records"));
Throws:
IllegalArgumentException - if preconditions on the uri parameter do not hold. The format of the URI is provider specific.
FileSystemNotFoundException - The file system, identified by the URI, does not exist and cannot be created automatically, or the provider identified by the URI's scheme component is not installed
SecurityException - if a security manager is installed and it denies an unspecified permission to access the file system
public static boolean isValid(Path p){
return p.startsWith("temp") && p.endsWith("clients.dat");
}
public static void writeData() {
Path p1 = Paths.get("\\temp\\records");
Path p2 = p1.resolve("clients.dat");
System.out.println(p2+" "+isValid(p2));
}
以上會出現
\temp\records\clients.dat false
重點在於startsWith
他比較會將\一起放進去如將上面的判斷改成
p.startsWith("\temp") && p.endsWith("clients.dat");
就會事true
Path p1 = Paths.get("temp\\records"); Path p2 = p1.resolve("clients.dat"); System.out.println("P2 startsWith:"+p2.startsWith("temp"));// true System.out.println("P2 startsWith:"+p2.endsWith("clients.dat")); //true
注意:startsWith比較特別他會看路徑的定一個名稱前面有沒有/如果有判斷時就要加/,如果沒有就不要亂加!!
System.out.println(p2.endsWith("clients.dat\\"));這樣也會回傳true要小心
System.out.println(p2.endsWith("\\clients.dat"));這樣會回傳false
Path p3 = Paths.get("C:\\aaa"); System.out.println(p3.getParent());顯示C:\\
System.out.println(p3.getParent().getParent());顯示null
Remember the following 4 points about Path.getName() method :
1. Indices for path names start from 0.
2. Root (i.e. c:\) is not included in path names.
3. \ is NOT a part of a path name.
4. If you pass a negative index or a value greater than or equal to the number of elements, or this path has zero name elements, java.lang.IllegalArgumentException is thrown. It DOES NOT return null.
跟Path有關的
Path path2 = Paths.get("\\AAA\\howard\\test9.txt");
Path path3 = Paths.get("\\testX.txt");
System.out.println("getRoot:"+path2.getRoot()) ;
System.out.println("getName:"+path2.getName(0));
System.out.println("subpath:"+path2.subpath(0, 2));
System.out.println("resolve:"+path2.resolve(path3));
System.out.println("relativize:"+path2.relativize(path3));
getRoot:\
getName:AAA
subpath:AAA\howard
resolve:\testX.txt
relativize:..\..\..\testX.txt
Path pp1 = Paths.get("\\aa\\ccc");
Path pp2 = Paths.get("\\aa");
System.out.println("Test:"+pp2.relativize(pp1));
Test:ccc
relativize
Path p1 = Paths.get("c:\\personal\\.\\photos\\..\\readme.txt");
Path p2 = Paths.get("c:\\personal\\index.html");
System.out.println(p1.relativize(p2));
Path p1 = Paths.get("joe");
Path p2 = Paths.get("sally");
// Result is ../sally Path p1_to_p2 = p1.relativize(p2); // Result is ../joe Path p2_to_p1 = p2.relativize(p1);
當變數A與B沒有其他目錄時,會預設設為兩個變數為相同目錄
以上程式翻成白話就是
當我在joe 目錄時要如何到sally目錄
當我在sally目錄時要如何到joe目錄
Path p1 = Paths.get("home");
Path p3 = Paths.get("home/sally/bar");
// Result is sally/bar
Path p1_to_p3 = p1.relativize(p3);
// Result is ../..
Path p3_to_p1 = p3.relativize(p1);
如果2個路徑一樣回傳empty的path當我在home目錄時要如何到home/sally/bar目錄
當我在home/sally/bar目錄時要如何到home目錄
記得不會在有一個/
Path path1 = Paths.get("D:","KKK");
Path path2 = Paths.get("C:","Test");
System.out.println(path1.relativize(path2));
拋出錯誤
Exception in thread "main" java.lang.IllegalArgumentException: 'other' has different root
Path path1 = Paths.get("./KKK");
Path path2 = Paths.get("C:","Test");
System.out.println(path1.relativize(path2));
Exception in thread "main" java.lang.IllegalArgumentException: 'other' is different type of Path
在不同的路徑或兩不同Root的Path不能互轉低!!!
resolve 使變成
For example, consider the following code snippet:
// Solaris
Path p1 = Paths.get("/home/joe/foo");
// Result is /home/joe/foo/bar
System.out.format("%s%n", p1.resolve("bar"));
or
// Microsoft Windows
Path p1 = Paths.get("C:\\home\\joe\\foo");
// Result is C:\home\joe\foo\bar
System.out.format("%s%n", p1.resolve("bar"));
resolve method returns the passed-in path:// Result is /home/joe
Paths.get("foo").resolve("/home/joe");
Path path1 = Paths.get("folder1", "sub1");
Path path2 = Paths.get("folder2", "sub2")
System.out.println( path1.resolve(path2)); //folder1\sub1\folder2\sub2
如果前後都是相對路徑,resolve會將前後串接,如上
Path pathx1 = Paths.get("C:","howard","joe","foo");
Path pathx2 = Paths.get("D:","bar","bar2");
System.out.println(pathx1.resolve(pathx2));//D:\bar\bar2
如果前後都是絕對路徑,resolve會將後取代前!
只要是絕對路徑在後就附蓋!
resolveSibling
System.out.println( path1.resolveSibling(path2)); //folder1\folder2\sub2
如果想串接在父路徑下可使用resolveSibling效果如上
normalize
Path path2 = Paths.get("/aaa/tmp/./aac");Path path3 = Paths.get("/aaa/tmp/../aac");System.out.println(path2.normalize());//aaa/tmp/aacSystem.out.println(path3.normalize());//aaa/aacsubpath
顯示元素不包含ROOT
aaa = index 0
tmp = index 1
aac = index 2subpath參數包含第一個參數的index不包含第二個index1. Indexing starts from 0. 2. Root (i.e. c:\) is not considered as the beginning.不包含root 3. name at beginIndex is included but name at endIndex is not. 4. paths do not start or end with \.顯示頭尾\Path path2 = Paths.get("/aaa/tmp/aac");System.out.println(path2.subpath(0, 3));顯示aaa/tmp/aacSystem.out.println(path2.subpath(1, 3));顯示tmp/aac
toRealPath
- If
trueis passed to this method and the file system supports symbolic links, this method resolves any symbolic links in the path. - If the
Pathis relative, it returns an absolute path. - If the
Pathcontains any redundant elements, it returns a path with those elements removed.
This method throws an exception if the file does not exist or cannot be accessed. You can catch the exception when you want to handle any of these cases. For example:
try {
Path fp = path.toRealPath();
} catch (NoSuchFileException x) {
System.err.format("%s: no such" + " file or directory%n", path);
// Logic for case when file doesn't exist.
} catch (IOException x) {
System.err.format("%s%n", x);
// Logic for other sort of file error.
}
equals 與 compareTo 是比較路徑的字串
所以如果有一個是Path絕對路徑另一Path是相對路徑相比會回傳fasle與-1解法將相對路徑路徑的Path改成絕對路徑(toAbsolutePath())ROOT
Path path1 = Paths.get("./KKK");
System.out.println(path1.getRoot());
顯示null
沒有留言:
張貼留言