所以这是我的代码到目前为止,是的,我知道这是非常基本的。但我想要完成的是替换位于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);
}
}