java.lang.UnsupportedOperationException:“POSIX:权限”

2019-07-18 10:44发布

我使用的Java 7的文件API。 我写的是工作的罚款Ubuntu上创建目录完美的一类,但是当我在Windows上运行相同的代码,然后将其抛出错误:

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

我该如何解决这个问题?

Answer 1:

您可以使用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

视窗可惜的是不支持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");



文章来源: java.lang.UnsupportedOperationException: 'posix:permissions' not supported as initial attribute on Windows