在 Java 中操作 Windows 文件权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1198517/
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
Manipulating Windows file permissions in Java
提问by anoop john
Using Java how could I manipulate the access permissions of a file in Windows?
使用 Java 如何在 Windows 中操作文件的访问权限?
回答by Zed
If you are using Java 6, File class gives you setExecutable, setWritable, etc. See: http://java.sun.com/javase/6/docs/api/java/io/File.html
如果您使用的是 Java 6,则 File 类为您提供 setExecutable、setWritable 等。请参阅:http: //java.sun.com/javase/6/docs/api/java/io/File.html
On older Java versions this is not possible; you have to exec OS commands to do that:
在较旧的 Java 版本上这是不可能的;你必须执行操作系统命令才能做到这一点:
Windows:
视窗:
Runtime.getRuntime().exec("attrib -r myFile");
Unix:
Unix:
Runtime.getRuntime().exec("chmod 777 myFile");
回答by Brian Agnew
The new Java 7 java.nio.file.attributepackage makes all of this a lot easier. It provides views onto the complete set of file attributes, including Posix file permissions.
新的 Java 7 java.nio.file.attribute包使这一切变得更加容易。它提供了完整的文件属性集的视图,包括Posix 文件权限。