【发布时间】:2012-06-02 18:19:21
【问题描述】:
我有以下sn-p的代码:
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 OS 中文件的权限吗?
【问题讨论】:
-
它如何在 Windows 7 上“不起作用”?
-
什么确切地说不起作用?
-
告诉你在运行上面的代码时得到什么文件权限的输出。没有那个什么都做不了。
-
方法 canXXX 始终返回 true,即使不应该返回。
-
你检查你的程序没有扩展权限吗?为什么你说它不应该返回 true ?以后在读或写时会出错吗?
标签: java file-permissions