简体   繁体   English

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

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

I am using Java 7 File API.我正在使用 Java 7 文件 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:我写了一个在 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 createFolder method of Folder .错误来自Folder createFolder方法。

How do I resolve this error?如何解决此错误?

You use PosixFilePermission which can be used only with operating systems which are compatibile with POSIX:您使用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.一种文件属性视图,它提供通常与文件系统上的文件相关联的文件属性视图,这些文件系统由实现便携式操作系统接口 (POSIX) 标准系列的操作系统使用。

Operating systems that implement the POSIX family of standards commonly use file systems that have a file owner, group-owner, and related access permissions.实现 POSIX 系列标准的操作系统通常使用具有文件所有者、组所有者和相关访问权限的文件系统。 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.不幸的是,Windows 不支持 POSIX 文件系统,所以这就是您的代码不起作用的原因。 In order to create a directory in Windows you should use:为了在 Windows 中创建目录,您应该使用:

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

The / will be automatically changed to \\ in Windows.在 Windows 中/将自动更改为\\ If you want to create the whole path at once you have to use mkdirs() method.如果你想一次创建整个路径,你必须使用mkdirs()方法。 More info: http://docs.oracle.com/javase/6/docs/api/java/io/File.html更多信息: 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() .为了在 Windows 中设置文件权限,您必须使用setReadable()setWritable()setExecutable() That are File class methods and set only file owner's permissions.那是File类方法,只设置文件所有者的权限。 Note that mentioned methods were added in Java 1.6.请注意,上述方法是在 Java 1.6 中添加的。 In older versions you would have to use (Windows version):在旧版本中,您必须使用(Windows 版本):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “ java.lang.UnsupportedOperationException:尚不支持。” - “java.lang.UnsupportedOperationException: Not supported yet.” “java.lang.UnsupportedOperationException:尚不支持。” - “java.lang.UnsupportedOperationException: Not supported yet.” java.lang.UnsupportedOperationException:尚不支持 - java.lang.UnsupportedOperationException: Not supported yet Java Dropbox HttpServletRequest(java.lang.UnsupportedOperationException:尚不支持。) - Java Dropbox HttpServletRequest ( java.lang.UnsupportedOperationException: Not supported yet.) 由java.lang.UnsupportedOperationException引起:AdapterView不支持addView(View,layoutParams) - Caused by java.lang.UnsupportedOperationException: addView(View,layoutParams) is not supported in the AdapterView DBFit java.lang.UnsupportedOperationException: 不支持类型 İNT - DBFit java.lang.UnsupportedOperationException: Type İNT is not supported java.lang.UnsupportedOperationException:option不是受支持的字段类型 - java.lang.UnsupportedOperationException: option is not a supported field type Duke Fast Deduplication:java.lang.UnsupportedOperationException:尚不支持操作? - Duke Fast Deduplication: java.lang.UnsupportedOperationException: Operation not yet supported? Tomcat连接池-java.lang.UnsupportedOperationException:BasicDataSource不支持 - Tomcat Connection Pooling - java.lang.UnsupportedOperationException: Not supported by BasicDataSource java.lang.UnsupportedOperationException: DROP CONSTRAINT 仅支持 Hibernate - java.lang.UnsupportedOperationException: DROP CONSTRAINT is only supported for Hibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM