在 Java 中获取主机文件位置

发布于 2024-10-04 03:02:00 字数 116 浏览 0 评论 0原文

在不同平台上使用我的应用程序时,如何获取 hosts 文件 的位置?

How can I get the location of the hosts file when using my application on different platforms?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

注定孤独终老 2024-10-11 03:02:01

恐怕你必须在这里自己动手,因为这是一个相当低级的系统功能,我认为它有点超出了你期望 java 为你做的事情。

此链接指向一些特殊选项,您可以设置这些选项来更改绑定顺序,我不认为他们会告诉您主机文件在哪里,但您可以在这些选项及其周围进行调查,看看是否可以找到其他任何东西来帮助您

think that you would have to roll your own here I'm afraid, as this is a pretty low level system function, I think that it's a little beyond what you could expect java to do for you.

This link points to some special options that you can set to alter the bind order, I don't think that they will tell you where the hosts file is, but you could could investigate in and around these options to see if you can find anything else to help you

柠北森屋 2024-10-11 03:02:01

您可以使用“os.name”系统属性来确定操作系统。然后,根据每个操作系统存储主机文件的位置检索主机文件。例如:

String osName = System.getProperty("os.name").toLowerCase();
File hostsFile;
if (osName.startsWith("mac os x")){
  hostFile = new File("/etc/hosts");
} else if (osName.contains("windows")){
  hostFile = //...
}

You could use the "os.name" system property to determine the operating system. Then, retrieve the hosts file depending on where each operating system stores it. For example:

String osName = System.getProperty("os.name").toLowerCase();
File hostsFile;
if (osName.startsWith("mac os x")){
  hostFile = new File("/etc/hosts");
} else if (osName.contains("windows")){
  hostFile = //...
}
我的影子我的梦 2024-10-11 03:02:01

Wikipedia 上查看每个操作系统和版本的主机文件位置。不幸的是,您必须实现自己的逻辑,类似于迈克尔的答案:

import java.awt.Desktop;
import java.io.File;

public class HostsFile
{
    public static void main(String[] args) throws Exception
    {
        String osName = System.getProperty("os.name");

        File hostsFile = null;

        if (osName.contains("Windows"))
        {
            hostsFile = new File(System.getenv("WinDir")
                    + "\\system32\\drivers\\etc\\hosts");
        }

        Desktop.getDesktop().open(hostsFile);
    }
}

Take a look at the hosts file locations for each operating system and version on Wikipedia. Unfortunately you will have to implement your own logic similar to Michael's answer:

import java.awt.Desktop;
import java.io.File;

public class HostsFile
{
    public static void main(String[] args) throws Exception
    {
        String osName = System.getProperty("os.name");

        File hostsFile = null;

        if (osName.contains("Windows"))
        {
            hostsFile = new File(System.getenv("WinDir")
                    + "\\system32\\drivers\\etc\\hosts");
        }

        Desktop.getDesktop().open(hostsFile);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文