Java 配置文件不特定于目录

发布于 2024-11-29 14:46:37 字数 234 浏览 5 评论 0原文

我在我的 java 应用程序中使用配置 (.ini) 文件。目前我专门这样调用dll的位置。

Properties p = new Properties();
p.load(new FileInputStream("C:\\myconfigfile.ini");

我希望拥有它,这样用户就不必在程序运行之前将其放入某个目录中。

有没有办法实现这一点,或者这只是最佳实践。

I am using a configuration (.ini) file with my java app. At the moment I am specifically calling the location of the dll like this.

Properties p = new Properties();
p.load(new FileInputStream("C:\\myconfigfile.ini");

I would like to have it so the user does not have to put it into a certain directory before the program operates.

Is there a way to implement this or is this just the best practice to go with.

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

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

发布评论

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

评论(4

羁客 2024-12-06 14:46:37

将其放入 CLASSPATH 中并使用类加载器将资源作为流加载。即使对于 WAR 文件或 JAR 中的 Web 应用程序,这也始终有效。

Put it in the CLASSPATH and load resource as stream using the class loader. That'll always work, even for web apps in WAR files or JARs.

七颜 2024-12-06 14:46:37

您应该将您的 ini 放在资源文件夹中并将它们作为资源加载。这是一个优秀教程来帮助您入门。

You should put your ini in a resources folder and load them as resources. Here's an excellent tutorial to get you started.

§对你不离不弃 2024-12-06 14:46:37

在根目录下创建一个 ini 文件夹,并将其放入其中。然后当你开始你的程序时,这一切都与你从哪里开始有关。

Properties p = new Properties();
p.load(new FileInputStream(".\\ini\\myconfigfile.ini");

make an ini folder in your root directory, and put it in there. Then when you start your program, it's all relative to where you start from.

Properties p = new Properties();
p.load(new FileInputStream(".\\ini\\myconfigfile.ini");
[浮城] 2024-12-06 14:46:37

查看 Apache Commons 配置
它解决了有关配置文件的许多问题。它还支持为每个用户进行单独的配置,这对于团队的开发非常重要。

Have a look at Apache Commons Configuration.
It is solving many issues around configuration files. It also supports having individual configs for each user what is imho very important for development in a team.

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