有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java AccessDeniedException:C:\Windows\System32\drivers\etc\hosts

我正在尝试制作一个java程序,在web浏览器中阻止facebook页面。我试图以某种方式覆盖主机文件,但该文件已禁用覆盖。我试图将他复制到我的桌面上,然后添加一行内容来阻止页面,然后复制到etc文件夹并单击以复制(或覆盖)文件。但我不能用java来实现,我所做的只是在同一个文件夹中创建另一个文件并在其中添加行。但是我无法将新文件复制到旧文件,我不知道如何操作,这是我的代码,我正在等待解决方案:)

public class Sandbox {

    private final static File zdroj = new File("C:\\Windows\\System32\\drivers\\etc\\hosts");
    private final static File ciel = new File("C:\\Windows\\System32\\drivers\\etc\\hostsTemp");

    public static void main(String[] args) {

    try {
        Files.copy(zdroj.toPath(), ciel.toPath());
        PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(ciel, true)));
        writer.append("\n\n127.0.0.1 facebook.com www.facebook.com http://www.facebook.com/ http://facebook.com");
        writer.close();
        Files.delete(zdroj.toPath());
        Files.copy(ciel.toPath(), zdroj.toPath());
    } catch (IOException ex) {
        Logger.getLogger(Sandbox.class.getName()).log(Level.SEVERE, null, ex);
    }

    }

}

共 (1) 个答案

  1. # 1 楼答案

    您需要以提升的权限运行应用程序。尝试使用某个管理员用户启动它