java 从 config.properties 文件中读取 Maven 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35008377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:38:15  来源:igfitidea点击:

Reading from config.properties file Maven project

javamaven

提问by user5520049

I'm new in maven and java , i imported maven project , it has config.properties file , this file contains

我是 maven 和 java 新手,我导入了 maven 项目,它有 config.properties 文件,这个文件包含

PATH_TO_QUERY_FILE=abc.txt

and class that using the property from the config.properties like

和使用 config.properties 中的属性的类

queryFile = prop.getProperty("PATH_TO_QUERY_FILE");

Should i add real path for this file in config to make code read it while running ? like

我应该在配置中添加这个文件的真实路径,让代码在运行时读取它吗?喜欢

PATH_TO_QUERY_FILE=/home/user/workspace/project/abc.txt 

or not ?

或不 ?

  • when i mvn packageproject i didn't find this files in the jar file which is created , Is this because of real path that i'm asking about or not ?
  • 当我进行mvn package项目时,我没有在创建的 jar 文件中找到这些文件,这是因为我询问的真实路径吗?

enter image description here

在此处输入图片说明

Coder used this statements

编码器使用此语句

FileInputStream finputstream = new FileInputStream( "/home/user/workspace/project/config.properties"); prop.load(finputstream); queryFile = prop.getProperty("PATH_TO_QUERY_FILE"); fos = new FileOutputStream(new File( prop.getProperty("PATH_TO_OUTPUT_FILE"))); finputstream.close();

FileInputStream finputstream = new FileInputStream( "/home/user/workspace/project/config.properties"); prop.load(finputstream); queryFile = prop.getProperty("PATH_TO_QUERY_FILE"); fos = new FileOutputStream(new File( prop.getProperty("PATH_TO_OUTPUT_FILE"))); finputstream.close();

回答by Dzmitry Paulenka

It depends on how you're planning to distribute your application. And on how you're reading the files. Basically there'are two options: reading from files and reading from classpath.

这取决于您计划如何分发您的应用程序。以及您如何阅读文件。基本上有两种选择:从文件中读取和从类路径中读取。

1. Reading from files.

1. 从文件中读取。

If you're using this path by passing it to a Fileconstructor or passing to some FileStream, than you should know that your paths will be resolved relative to working folder. Working folder is basically the one, from which you initiate java -cp .. some.MainClasscommand. In that case you will have to copy config.propertiesfile with you and run your command from appropriate directory. File configs are more flexible in a way, that you will not have to rebuild your application (or repack manually), if you will want to change some values in that config. If you're not really planning to change values after distribution, than usually it's better to pack it in jars and read as a resource.

如果您通过将其File传递给构造函数或传递给某个 FileStream来使用此路径,那么您应该知道您的路径将相对于工作文件夹进行解析。工作文件夹基本上是您从中启动java -cp .. some.MainClass命令的文件夹。在这种情况下,您必须随身复制config.properties文件并从适当的目录运行您的命令。文件配置在某种程度上更灵活,如果您想更改该配置中的某些值,您将不必重建您的应用程序(或手动重新打包)。如果您真的不打算在分发后更改值,通常最好将其打包在 jar 中并作为资源读取。

2. Reading from resources.

2. 从资源中读取。

Java has a mechanism to read resources from classpath. To do so, you first need to pack resources in your usual jars, that goes to classpath. Then you can load them in your programm by obtaining the appropriate classloader reference. Usually you just use any class in context:

Java 有一种从类路径读取资源的机制。为此,您首先需要将资源打包在常用的 jar 中,即进入类路径。然后您可以通过获取适当的类加载器引用将它们加载到您的程序中。通常你只是在上下文中使用任何类:

InputStream s = AnyClass.class.getClassLoader().getResourceAsStream("/config.properties");
//or shortcut version
InputStream s = AnyClass.class.getResourceAsStream("/config.properties");

//now can use this input stream as usually, i.e. to load as properties
Properties props = new Properties();
props.load(inputStream);

Now for your question on how to use this resource loading properly with maven. By default maven packs all files in <MODULE_DIR>/src/main/resourcesinto the resulting jar. So all you need is to place your abc.txtinto <MODULE_DIR>/src/main/resources/whatever/abc.txtand load it as ...getResourceAsStream("/whaterver/abc.txt"). That will work no matter, how you're running your app: from IDE or after packaging. Than do with it whatever you've done.

现在关于如何使用 maven 正确使用此资源加载的问题。默认情况下,maven 将所有文件打包<MODULE_DIR>/src/main/resources到生成的 jar 中。所以你所需要的就是将你的abc.txt放入<MODULE_DIR>/src/main/resources/whatever/abc.txt并加载为...getResourceAsStream("/whaterver/abc.txt"). 无论您如何运行您的应用程序,这都将起作用:从 IDE 还是在打包后。不管你做过什么,都不要管它。