gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:54:03 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com