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

标签 java java-7 nio java-io unsupportedoperation

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

    }
}

错误来自 FoldercreateFolder 方法。

如何解决此错误?

最佳答案

您使用 PosixFilePermission,它只能用于与 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");

关于java.lang.UnsupportedOperationException : 'posix:permissions' not supported as initial attribute on Windows 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14415960/

相关文章:

java - Java中如何使用注解替换代码?

java - Java 7 中 Java 8 的功能接口(interface)

java - 另一个 RMI 问题

java - 无效的 HTTP 方法 : PATCH

java - 在 WIndows 上的 Java 中,如何检测文件是否具有 'Read Only' 属性

java - 在 Java 中为 JOGL 释放直接缓冲区 native 内存

java - Niolocker - 'filter' 和 'locker' 选项必须出现在提供的外部 'scanner' 上

java - char[] 或 StringBuilder 用于密码?

java - 您是否需要在非驱动程序类中导入语句?

java - JButton 不起作用?