到目前为止,这是我的代码,是的,我知道它非常基本。但我想要完成的是替换位于 System32\drivers\etc 文件夹中的主机文件,我在这样做时遇到的问题是我收到拒绝访问错误。如何让程序访问以在 windows 文件夹中进行更改,SaveURL 类也只是下载文件并使用两个字符串通过函数 saveImage 传递名称和下载位置。我怎样才能给自己访问权限?在此先感谢我真的很感激。
private void jButton7MouseClicked(java.awt.event.MouseEvent evt) {
String destinationHosts = "hosts";
String urlHosts = "https://dl.dropbox.com/s/awdvoprxyo7r2q6/hosts?dl=1";
Object hostOptions[] = {"Replace", "Close"};
File fileHosts = new File(destinationHosts);
File dirHost = new File("hosts");
boolean dirHosts = dirHost.mkdir();
try {
Runtime.getRuntime().exec("notepad.exe \\Windows\\system32\\drivers\\etc\\hosts");
jTextArea2.append("Opening Host File \n");
int hostFile = JOptionPane.showOptionDialog(rootPane, "Would you like to replace the Hosts File?",
"Yes or No", JOptionPane.YES_NO_OPTION, JOptionPane.NO_OPTION, null, hostOptions, hostOptions[0]);
if (hostFile == JOptionPane.YES_NO_OPTION) {
jTextArea2.append("Downloading New Host File");
SaveUrl.saveImage(urlHosts, destinationHosts);
fileHosts.renameTo(new File(dirHost, fileHosts.getName()));
Path source = Paths.get("\\hosts\\hosts");
Path target = Paths.get("\\Windows\\system32\\drivers\\etc\\");
Files.copy(source, target);
jTextArea2.append("Host file replaced");
}
} catch (IOException ex) {
Logger.getLogger(ToolKit1.class.getName()).log(Level.SEVERE, null, ex);
}
}