2014-02-18 15 views
0

我想檢查文件夾的給定文件夾路徑(本地路徑,共享路徑)和憑證,無論用戶是否有權訪問此文件夾。如何在使用java的窗口上獲取文件夾安全屬性

在Windows中,我們可以驗證通過右鍵單擊該文件夾並轉到安全選項卡。

我看了一些文章,但其中大多數都建議在該文件夾中創建一個文件,然後刪除它來驗證它。我被限制在我的要求中這樣做。

請幫助我,如果我們能做到這一點它在Java

回答

1
  1. 創建具有給定路徑中的新文件:

    文件TESTFILE =新的文件(「路徑」);

  2. 如果的CanRead:

    如果(file.canRead()){// 做一些 }

有關可以與java.io.File中做什麼的完整列表,請訪問API(你可以try/catch語句刪除,寫等):

http://docs.oracle.com/javase/6/docs/api/java/io/File.html

1

File類有幾個有用的方法來實現你想要的:

File file = new File("path-to-file"); 
System.out.println(file.exists()); 
System.out.println(file.getAbsolutePath()); 
System.out.println(file.getParent()); 
System.out.println(file.canRead()); 
System.out.println(file.canWrite()); 
System.out.println(file.isHidden()); 

欲瞭解更多信息,請嘗試使用官方的文檔:http://docs.oracle.com/javase/6/docs/api/java/io/File.html


編輯

按OP的評論,這裏的一種檢查文件所有者和文件權限的方法:

Path path = FileSystems.getDefault().getPath("path"); 
    UserPrincipal owner = null; 
    Set<PosixFilePermission> posixFilePermissions = null; 
    try { 
     owner = Files.getOwner(path); 
     posixFilePermissions = Files.getPosixFilePermissions(path); 
    } catch (IOException ex) { 
     Logger.getLogger(TestCenter.class.getName()).log(Level.SEVERE, null, ex); 
    } 

    System.out.println(owner); 
    System.out.println(posixFilePermissions); 

輸出(該文件有664允許我的Linux機器上):

victor 
[OWNER_WRITE, OWNER_READ, OTHERS_READ, GROUP_WRITE, GROUP_READ] 
+0

這幾乎是相同的我先前的響應 – Matt

+1

@馬特嗯。真正。我認爲我應該舉一些例子,因爲從OP表達自己的方式來看,他似乎沒有太多經驗,也許不會覺得瀏覽文檔很容易。如果我在路上傷害了一些道德操守,我很抱歉。 – victorantunes

+0

不會傷害我的感受......你的例子也不錯。重點是幫助他人。 – Matt

相關問題