2013-01-19 65 views
11

我正在使用Java 7 File API。我寫的是工作的罰款Ubuntu上創建目錄完美的一類,但是當我在Windows上運行相同的代碼則拋出錯誤:java.lang.UnsupportedOperationException:作爲Windows上的初始屬性不支持'posix:permissions'

Exception in thread "main" java.lang.UnsupportedOperationException: 'posix:permissions' not supported as initial attribute 
    at sun.nio.fs.WindowsSecurityDescriptor.fromAttribute(Unknown Source) 
    at sun.nio.fs.WindowsFileSystemProvider.createDirectory(Unknown Source) 
    at java.nio.file.Files.createDirectory(Unknown Source) 
    at java.nio.file.Files.createAndCheckIsDirectory(Unknown Source) 
    at java.nio.file.Files.createDirectories(Unknown Source) 
    at com.cloudspoke.folder_permission.Folder.createFolder(Folder.java:27) 
    at com.cloudspoke.folder_permission.Main.main(Main.java:139) 

我的文件夾類代碼爲

package com.cloudspoke.folder_permission; 

import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.attribute.FileAttribute; 
import java.nio.file.attribute.PosixFilePermission; 
import java.nio.file.attribute.UserPrincipal; 
import java.util.Set; 

public class Folder{ 
    // attributes required for creating a Folder 
    private UserPrincipal owner; 
    private Path folder_name; 
    private FileAttribute<Set<PosixFilePermission>> attr; 


    public Folder(UserPrincipal owner,Path folder_name,FileAttribute<Set<PosixFilePermission>> attr){ 
     this.owner=owner; 
     this.folder_name=folder_name; 
     this.attr=attr; 
    } 
    //invoking this method will create folders 
    public void createFolder(){ 
     try { 
      //createDirectories function is used for overwriting existing folder instead of createDirectory() method 
      Files.createDirectories(folder_name, attr); 
      Files.setOwner(folder_name, owner); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     System.out.println("created Folder "+this.folder_name); 

    } 
} 

的錯誤是來自createFolder方法Folder

如何解決此錯誤?

+2

的Windows = Posix的。你爲什麼期望這個工作? – EJP

回答

15

您使用PosixFilePermission只能與操作系統這是compatibile與POSIX使用:

A file attribute view that provides a view of the file attributes commonly associated with files on file systems used by operating systems that implement the Portable Operating System Interface (POSIX) family of standards.

Operating systems that implement the POSIX family of standards commonly use file systems that have a file owner, group-owner, and related access permissions. This file attribute view provides read and write access to these attributes

的Windows可惜的是不支持POSIX文件系統,所以這就是爲什麼你的代碼不起作用。爲了在Windows中創建一個目錄,你應該使用:

new File("/path/to/folder").mkdir();

/將在Windows自動變爲\。如果您想一次創建整個路徑,則必須使用mkdirs()方法。更多信息:http://docs.oracle.com/javase/6/docs/api/java/io/File.html

爲了在Windows中設置文件權限,您必須使用setReadable()setWritable()setExecutable()。這是File類方法並僅設置文件所有者的權限。請注意,上述方法是在Java 1.6中添加的。在舊版本中,你將不得不使用(Windows版):

Runtime.getRuntime().exec("attrib -r myFile");

+0

是的,我想知道什麼應該是在Windows中創建目錄的代碼! –

+0

嘿,不建議使用'文件':( – fge

+0

和一個更可能是最後一個如何在上面的代碼中更改文件的所有者的名稱??請回復!! –