对java属性文件位置感到困惑

编程入门 行业动态 更新时间:2024-10-23 19:35:17
本文介绍了对java属性文件位置感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有简单的java项目结构:

I have simple java project with structure:

package com.abc: a.java b。 java c.properties

我在c.properties文件中配置了数据库配置参数。 在a.java和b.java中,我使用以下方法加载属性文件:

I have database configuration parameters configured in c.properties file. Inside a.java and b.java, I am loading properties file using:

Properties p = new Properties(); InputStream in = this.getClass().getResourceAsStream("c.properties"); p.load(in);

这很好用。但主要问题是,一旦我通过导出此代码准备可执行jar,属性文件也会打包在jar文件中。如果其他人想要修改不同数据库配置的属性文件,他怎么能这样做? 我是否必须将属性文件存储在本地计算机的某个固定位置。例如C:/。然后将jar和属性文件一起提供给另一个人。然后他需要复制C:/ location中的属性文件? 还有一个问题,我怎样才能使这个位置通用于Windows和Linux机器?

This works fine. But the main question is, once I prepare executable jar by exporting this code, properties file also gets packaged in jar file. If someone else wants to modify properties file for different database configuration, how can he do it? Do I have to store properties file in some fixed location in local machine. e.g. "c:/". Then give jar along with properties file to the other person. Then he needs to copy properties file inside C:/ location? Also one more question, how can i make this location generic for windows and linux machine?

推荐答案

典型方式处理此问题的方法是从嵌入式文件加载基本属性,并允许应用程序的用户指定包含替代的其他文件。一些伪代码:

The typical way of handling this is to load the base properties from your embedded file, and allow users of the application to specify an additional file with overrides. Some pseudocode:

Properties p = new Properties(); InputStream in = this.getClass().getResourceAsStream("c.properties"); p.load(in); String externalFileName = System.getProperty("app.properties"); InputStream fin = new FileInputStream(new File(externalFileName)); p.load(fin);

您的程序将被调用类似于:

Your program would be invoked similar to this:

java -jar app.jar -Dapp.properties="/path/to/custom/app.properties"

更多推荐

对java属性文件位置感到困惑

本文发布于:2023-08-02 17:57:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279909.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:困惑   属性   位置   文件   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!