在Java中替换主机文件

在Java中替换主机文件,java,windows,file,host,access-denied,Java,Windows,File,Host,Access Denied,这是我到目前为止的代码,是的,我知道它非常基本。但我想完成的是替换位于System32\drivers\etc文件夹中的主机文件,这样做的问题是我遇到了一个拒绝访问错误。如何授予程序在windows文件夹中进行更改的权限,同时类SaveURL只下载文件,并使用两个字符串通过函数saveImage传递名称和下载位置。我怎样才能让自己进入?提前谢谢,我真的很感激 private void jButton7MouseClicked(java.awt.event.MouseEvent evt) {

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

在尝试修改主机时,您自然会遇到权限问题。您是否以管理员身份运行该程序**我不相信使用JAVA,出于安全原因,您将能够编辑/替换文件。这可能会对您有所帮助,只需检查一下,谢谢您的帮助,上面的链接也很有帮助,但不一定能解决我的问题。