我创建了一个临时文件夹,然后尝试在我的Windows 7计算机中更改其权限。我有管理员权限。无法通过Java代码更改文件夹的权限
public class FilePermissionExample
{
public static void main(String[] args)
{
try {
File file = File.createTempFile("temp", Long.toString(System.nanoTime()));
file.delete();
file.mkdir();
if(file.exists()){
System.out.println("Is Execute allow : " + file.canExecute());
System.out.println("Is Write allow : " + file.canWrite());
System.out.println("Is Read allow : " + file.canRead());
}
file.setExecutable(false);
file.setReadable(false);
file.setWritable(false);
System.out.println("Is Execute allow : " + file.canExecute());
System.out.println("Is Write allow : " + file.canWrite());
System.out.println("Is Read allow : " + file.canRead());
if (file.createNewFile()){
System.out.println("File is created!");
}else{
System.out.println("File already exists.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出:
是execute允许:真
是写允许:真
是读允许:真
是execute允许:真
被写允许:真
被读允许: true
文件已存在。
预计:
是执行允许:真
被写允许:真
是读允许:真
是execute允许:假
被写允许:假
是读允许:假
我在使用hiverunner在windows中编写配置单元测试时遇到类似的问题。任何人都可以提出建议
什么返回方法setExecutable,setReadable和setWritable?如果使用setExecutable移除字符串会发生什么? –
它们都返回布尔值。 '假'正在被所有制定者返回。 我不明白你的第二个问题。这些方法将布尔作为参数而不是字符串。我没有使用字符串 – user2150837
如果操作系统不支持操作,这些操作可能会失败。我不知道如果Windows支持的可执行文件,所以我认为不支持和setExecutable方法失败的所有三个操作。 –