java.lang.UnsupportedOperationException: 'posix:permissions' 不支持作为 Windows 上的初始属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14415960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:11:42  来源:igfitidea点击:

java.lang.UnsupportedOperationException: 'posix:permissions' not supported as initial attribute on Windows

javajava-7niojava-iounsupportedoperation

提问by Ritesh Mehandiratta

I am using Java 7 File API. I wrote a class that is working fine on Ubuntu creating directories perfectly, but when I run same code on Windows then it is throwing error:

我正在使用 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)

My Folder class code is

我的文件夹类代码是

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);

    }
}

The error is coming from createFoldermethod of Folder.

错误来自 的createFolder方法Folder

How do I resolve this error?

如何解决此错误?

回答by Adam Sznajder

You use PosixFilePermissionwhich can be used only with operating systems which are compatibile with POSIX:

您使用PosixFilePermissionwhich 只能用于与 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.

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

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 unfortunatelly doesn't support POSIX file systems so this is why your code doesn't work. In order to create a directory in Windows you should use:

不幸的是,Windows 不支持 POSIX 文件系统,所以这就是您的代码不起作用的原因。为了在 Windows 中创建目录,您应该使用:

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

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

The /will be automatically changed to \in Windows. If you want to create the whole path at once you have to use mkdirs()method. More info: http://docs.oracle.com/javase/6/docs/api/java/io/File.html

/会自动更改为\在Windows中。如果你想一次创建整个路径,你必须使用mkdirs()方法。更多信息:http: //docs.oracle.com/javase/6/docs/api/java/io/File.html

In order to set file permissions in Windows you have to use setReadable(), setWritable()and setExecutable(). That are Fileclass methods and set only file owner's permissions. Note that mentioned methods were added in Java 1.6. In older versions you would have to use (Windows version):

为了在 Windows 中设置文件权限,您必须使用setReadable(),setWritable()setExecutable()。那是File类方法,只设置文件所有者的权限。请注意,上述方法是在 Java 1.6 中添加的。在旧版本中,您必须使用(Windows 版本):

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

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