我尝试创建一个目录,但我不断得到AccessDeniedException。我怎样才能进入?
代码:String pathForMac = System.getProperty("user.home") + "Library\Application Support\Petatech\Zindroid";
Path macPath = Paths.get(pathForMac);{
if (!Files.exists(macPath)) {
try {
Files.createDirectory(macPath);
} catch (Exception e) {
System.out.println(e);
JOptionPane.showMessageDialog(null, e.toString(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
你可以使用特权块API来做到这一点。像这样的东西应该可以作为System工作。由于缺乏权限,getProperty(String key)可能会抛出AccessDeniedException。
String pathForMac = java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<String>() {
public String run() {
return System.getProperty("user.home");
}
});
//Append the rest to the String
pathForMac += "Library\Application Support\Petatech\Zindroid";