读取JAR文件外的属性文件 [英] Read properties file outside JAR file

查看:102
本文介绍了读取JAR文件外的属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JAR文件,我的所有代码都已归档以便运行。我必须访问每次运行前需要更改/编辑的属性文件。我想将属性文件保存在JAR文件所在的目录中。有没有告诉Java从该目录中获取属性文件?

I have a JAR file where all my code is archived for running. I have to access a properties file which need to be changed/edited before each run. I want to keep the properties file in the same directory where the JAR file is. Is there anyway to tell Java to pick up the properties file from that directory ?

注意:我不想将属性文件保留在主目录中或传递路径命令行参数中的属性文件。

Note: I do not want to keep the properties file in home directory or pass the path of the properties file in command line argument.

推荐答案

所以,你想要处理你的 .properties 文件与main / runnable jar相同的文件夹作为文件而不是main / runnable jar的资源。在这种情况下,我自己的解决方案如下:

So, you want to treat your .properties file on the same folder as the main/runnable jar as a file rather than as a resource of the main/runnable jar. In that case, my own solution is as follows:

第一件事:您的程序文件架构应该是这样的(假设您的主程序是main.jar及其主要内容)属性文件是main.properties):

First thing first: your program file architecture shall be like this (assuming your main program is main.jar and its main properties file is main.properties):

./ - the root of your program
 |__ main.jar
 |__ main.properties

使用此架构,您可以修改main中的任何属性。在main.jar运行之前或期间使用任何文本编辑器的属性文件(取决于程序的当前状态),因为它只是一个基于文本的文件。例如,您的main.properties文件可能包含:

With this architecture, you can modify any property in the main.properties file using any text editor before or while your main.jar is running (depending on the current state of the program) since it is just a text-based file. For example, your main.properties file may contain:

app.version=1.0.0.0
app.name=Hello

那么,当你从root / base文件夹运行你的主程序时,通常你会运行它像这样:

So, when you run your main program from its root/base folder, normally you will run it like this:

java -jar ./main.jar

或者,马上:

java -jar main.jar

在main.jar中,您需要为main中的每个属性创建一些实用程序方法。属性文件;假设 app.version 属性将具有 getAppVersion()方法,如下所示:

In your main.jar, you need to create a few utility methods for every property found in your main.properties file; let say the app.version property will have getAppVersion() method as follows:

/**
 * Gets the app.version property value from
 * the ./main.properties file of the base folder
 *
 * @return app.version string
 * @throws IOException
 */
public static String getAppVersion() throws IOException{

    String versionString = null;

    //to load application's properties, we use this class
    Properties mainProperties = new Properties();

    FileInputStream file;

    //the base folder is ./, the root of the main.properties file  
    String path = "./main.properties";

    //load the file handle for main.properties
    file = new FileInputStream(path);

    //load all the properties from this file
    mainProperties.load(file);

    //we have loaded the properties, so close the file handle
    file.close();

    //retrieve the property we are intrested, the app.version
    versionString = mainProperties.getProperty("app.version");

    return versionString;
}

在主程序的任何需要的部分app.version value,我们调用它的方法如下:

In any part of the main program that needs the app.version value, we call its method as follows:

String version = null;
try{
     version = getAppVersion();
}
catch (IOException ioe){
    ioe.printStackTrace();
}

这篇关于读取JAR文件外的属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆