我有以下代码片段:
public class ExampleClass {
public static void main(String[] args) throws FileNotFoundException {
String filePath = args[0];
File file = new File(filePath);
if (!file.exists())
throw new FileNotFoundException();
if (file.canWrite())
System.out.println(file.getAbsolutePath() + ": CAN WRITE!!!");
else
System.out.println(file.getAbsolutePath() + ": CANNOT WRITE!!!!!");
if (file.canRead())
System.out.println(file.getAbsolutePath() + ": CAN READ!!!");
else
System.out.println(file.getAbsolutePath() + ": CANNOT READ!!!!!");
if (file.canExecute())
System.out.println(file.getAbsolutePath() + ": CAN EXECUTE!!!");
else
System.out.println(file.getAbsolutePath() + ": CANNOT EXECUTE!!!!!");
}
}
它可以在Linux操作系统中运行,但问题是它不能在Windows7中运行。所以问题是:有人知道一种方法来独立检查 Java 操作系统中文件的权限吗?
很难相信 Java
File.canXxxx()
方法在任何版本的 Windows 上都通常会被破坏。
但是,阅读此 Sun 错误报告...并哭泣。简而言之,这是一个 Windows 错误,Sun 决定不解决它。 (但是新的 Java 7 API do 可以工作...)
FWIW,我认为尝试检查这样的文件访问权限是不好的做法。最好只是尝试使用该文件,并在发生异常时捕获异常。请参阅https://stackoverflow.com/a/6093037/139985了解我的推理。 (现在我们还有另一个理由......)
import java.io.FileNotFoundException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PermissionCheck {
public static void main(String[] args) throws FileNotFoundException {
String filePath = args[0];
Path p = Paths.get(filePath);
if (Files.notExists(p))
throw new FileNotFoundException();
if (Files.isWritable(p))
...
if (Files.isReadable(p))
...
if (Files.isExecutable(p))
...
}
}
JDK:1.7.0_25、1.8.0_91
操作系统:Windows 7、8(64位)
其次,当我们检查远程驱动器上的文件权限时,仅在 Windows 资源管理器中设置它通常是不够的(
属性... - 只读/隐藏/存档等)。例如,我的组织有其他机制来控制本地和远程文件权限,即使是我的电脑的管理员也不能保证一切。即使您可以手动/以编程方式更改文件的权限,如果某些其他应用程序/组策略/等禁止您这样做,更改也可能会失败。 (例如,setReadable()
返回
false
,表明不可能)例如,我可以
execute
远程目录中的一个
txt
文件,意思是打开它,但是同目录中的
bat
文件不可执行,实际上,就我而言,当我想创建
bat
文件时,我需要要求我的管理员获得更多权限。我认为可能是
bat
扩展被禁止。因为作为Windows中某个用户组的用户,您的操作和您运行的JVM受到比JVM本身更高的规则的限制。如果我错了请纠正我。但是,即使您可能无法设置文件的权限,现在您也可以在 Java 7 中正确读取它们。显然,在错误报告之后,Java 人员已经做了一些事情来修复大部分问题。我正在使用
jdk 1.7.0_19
,为了测试,我做了以下操作:
Read Only
和
Hidden
。
setXxxxx()
方法可以工作)。
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FilePermissionTester {
public static void main( String[] args ) throws IOException {
File file = new File("Y:\\some\\remote\\drive\\directoy\\xxxxx.txt");
System.out.println( "exists:" + file.exists() );
System.out.println( "is file:" + file.isFile() );
System.out.println( "can read:" + file.canRead() );
System.out.println( "can execute:" + file.canExecute() );
System.out.println( "can write:" + file.canWrite() );
System.out.println( "is hidden:" + file.isHidden() );
System.out.println("change it to be unreadable, and it works? " + file.setReadable(false));
System.out.println( "can read:" + file.canRead() );
System.out.println("change it to be writable, and it works? " + file.setWritable(true));
System.out.println( "can write:" + file.canWrite() );
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read();
fileInputStream.close();
}
}
exists:true
is file:true
can read:true
can execute:true
can write:false
is hidden:true
change it to be unreadable, and it works? false
can read:true
change it to be writable, and it works? true
can write:true
现在我可以读取这个文件,编辑它并保存它。在更改权限之前,我在保存时被要求
另存为..。
注意文件是可读的,setReadable(false)
返回
false
,文件仍然可读。 JavaDochere说,当用户无权更改访问权限时,或者当
setReadable()
已经是
false
时,
readable
返回
false
,并且底层系统没有对此的实现。调试 Java API 并不能提供太多信息,因为实现被标记为
native
并且看不到更多信息。但我有权更改可写性,所以这是我不明白的事情。但还要注意,
java.util.File
不支持更多属性,例如
setHidden()
。也许你可以在java.security
中查看其他包,例如AccessController?