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

查看:23
本文介绍了读取 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.

推荐答案

因此,您希望将与 main/runnable jar 位于同一文件夹中的 .properties 文件视为文件而不是主/可运行 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.jar 运行之前或期间使用任何文本编辑器修改 main.properties 文件中的任何属性(取决于程序的当前状态),因为它只是一个基于文本的文件.例如,您的 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

所以,当你从根目录/基本文件夹运行你的主程序时,通常你会像这样运行它:

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.properties 文件中的每个属性创建一些实用方法;假设 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
 */

import java.util.Properties;

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 值的主程序的任何部分,我们调用它的方法如下:

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天全站免登陆