2014-01-28 20 views
1

編輯:我知道FileSystem.getDefault()會給我我在原始問題陳述中尋找的東西。我正在嘗試使用FileSystem.getFileSystem(URI)獲取給定路徑的文件系統任意獲取任何給定路徑的FileStore對象

我正在嘗試開發一些代碼,它會給我一個給定路徑的java.nio.file.FileSystem對象。

下面是一些非常簡化的示例代碼給出正在嘗試什麼更好的主意:

public FileSystem getCwdFilesystem() 
{ 
    URI cwdUri = null; 
    String delimiter = ""; 
    try 
    { 
     _cwd = System.getProperty("user.dir"); 
     cwdUri = new URI("file", delimiter + _cwd, null); 
    } 
    catch (URISyntaxException ue) 
    { 
     System.out.println("URI Creation failure on URI: " + _cwd); 
     ue.printStackTrace(); 
     System.exit(1); 
    } 

    System.out.println("Filestore data for CWD: " + cwdUri.toString()); 

    return (FileSystems.getFileSystem(cwdUri)); 
} 

在執行時,一個例外是扔在代碼的最後一行:

Filestore data for CWD: file:/Users/redacted/Documents/Java%20Projects/ExampleCode 
Exception in thread "main" java.lang.IllegalArgumentException: Path component should be '/' 
    at sun.nio.fs.UnixFileSystemProvider.checkUri(UnixFileSystemProvider.java:77) 
    at sun.nio.fs.UnixFileSystemProvider.getFileSystem(UnixFileSystemProvider.java:92) 
    at java.nio.file.FileSystems.getFileSystem(FileSystems.java:217) 
    at examplecode.FilesystemCapacity.getCwdFilesystem(FilesystemCapacity.java:54) 
    at examplecode.FilesystemCapacity.main(FilesystemCapacity.java:33) 
Java Result: 1 

當我對分隔符變量進行小更新時:

String delimiter = "/"; 

我得到一個不同的錯誤消息來自同一個地方拋出:

Filestore data for CWD: file://Users/redacted/Documents/Java%20Projects/ExampleCode 
Exception in thread "main" java.lang.IllegalArgumentException: Authority component present 
    at sun.nio.fs.UnixFileSystemProvider.checkUri(UnixFileSystemProvider.java:73) 
    at sun.nio.fs.UnixFileSystemProvider.getFileSystem(UnixFileSystemProvider.java:92) 
    at java.nio.file.FileSystems.getFileSystem(FileSystems.java:217) 
    at examplecode.FilesystemCapacity.getCwdFilesystem(FilesystemCapacity.java:54) 
    at examplecode.FilesystemCapacity.main(FilesystemCapacity.java:33) 
Java Result: 1 

添加額外的「/」字符分隔符只需再次得到我的第一個錯誤消息。

我在做什麼錯?

+0

會發生什麼,如果你加「./」作爲分隔符? – mdewitt

+0

沒有快樂。錯誤消息更改爲「路徑組件未定義」。 –

+0

您應該使用File.separator而不是分隔符 –

回答

1

我發現了一個參考,我以前錯過了NIO.2文檔路徑的on the last page

我寫了一些測試代碼,這給了我正是我需要的:

public void getPathFilesystem(String path) 
    { 
     try 
     { 
      URI rootURI = new URI("file:///"); 
      Path rootPath = Paths.get(rootURI); 
      Path dirPath = rootPath.resolve(path); 
      FileStore dirFileStore = Files.getFileStore(dirPath); 

      printFileStore(dirFileStore, path); 
     } 
     catch (IOException | URISyntaxException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    public void printFileStore(FileStore filestore, String path) 
    { 
     try 
     { 
      System.out.println("Name: " + filestore.name()); 
      System.out.println("\tPath: " + path); 
      System.out.println("\tSize: " + filestore.getTotalSpace()); 
      System.out.println("\tUnallocated: " + filestore.getUnallocatedSpace()); 
      System.out.println("\tUsable: " + filestore.getUsableSpace()); 
      System.out.println("\tType: " + filestore.type()); 
     } 
     catch (IOException ioe) 
     { 
      ioe.printStackTrace(); 
     } 
    } 
+1

感謝您提及'resolve';文件確實說明這樣做很重要,但它有點模糊。 –

相關問題