2014-02-27 73 views
0

爲什麼會發生這種情況?java nio路徑無法處理windows網絡路徑

def path=java.nio.file.Paths.get("c:/kittuhomestore/Csmart/files/companies"); 
path.getNameCount(); 
4 

def path=java.nio.file.Paths.get("//kittuhomestore/Csmart/files/companies"); 
path.getNameCount(); 
2 

後者是一個Windows共享網絡驅動器。

+1

好像'/ kittuhomestore/Csmart'是網絡路徑,'/ files'是根目錄,'/ companies'是其中的一個目錄。 –

回答

2
Path path = java.nio.file.Paths.get("c:/kittuhomestore/Csmart/files/companies"); 
System.err.println(path.getRoot()); 

輸出:

c:\ 

在第一種情況中,路徑的RootC:\,因此剩下的部分是kittuhomestoreCsmartfilescompanies,因此組件。


Path path = java.nio.file.Paths.get("//kittuhomestore/Csmart/files/companies"); 
System.err.println(path.getRoot()); 

輸出:

\\kittuhomestore\Csmart\ 

在第二種情況下,路徑的Root\\kittuhomestore\Csmart\,因此剩下的部分是filescompanies,因此組件。

這是因爲UNC path具有格式

\\server\share\file_path 

其中\\server\share是路徑的根。

+0

出於好奇,你是否在自己的系統上設置了這樣一條路徑?它很簡單嗎? –

+0

不,它不是必需的 - 只創建一個'Path'對象還沒有訪問文件系統 –

+0

忘了這一點,謝謝。 –