在 Java 中获取主机文件位置
在不同平台上使用我的应用程序时,如何获取 hosts 文件 的位置?
How can I get the location of the hosts file when using my application on different platforms?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
恐怕你必须在这里自己动手,因为这是一个相当低级的系统功能,我认为它有点超出了你期望 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
您可以使用“os.name”系统属性来确定操作系统。然后,根据每个操作系统存储主机文件的位置检索主机文件。例如:
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:
在 Wikipedia 上查看每个操作系统和版本的主机文件位置。不幸的是,您必须实现自己的逻辑,类似于迈克尔的答案:
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: