[英]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.