简体   繁体   English

如何访问位于项目文件夹外部和项目类路径外部的.properties文件?

[英]How can I access to a .properties file situated outside the project folder and outside the project classpath?

I have the following problem trying to retrieve a config.properties file situated outside my project root directory. 我在尝试检索位于项目根目录之外的config.properties文件时遇到以下问题。

So I have a situation like this: 所以我有这样的情况:

CONTAINER DIRECTORY
     |
     |
     |------> config.properties
     |
     |
     |------>PROJECT-ROOT
                 |
                 |
                 |
                 |------> src
                     |
                     |
                     |------> mainPkg (the package name)
                                 |
                                 |
                                 |------> Main (the main class containing the main() method)

From the main() method situated inside the Main class I have to access to the config.properties file that is at the same level of the project root folder (and that is out of the project classpath). Main类内部的main()方法中,我必须访问config.properties文件,该文件位于项目根文件夹的同一级别(并且不在项目类路径中)。

I have to use relative path so I have to do something like this: starting from a folder inside the project I have to come back untill the CONTAINER DIRECTORY and here take the config.properties file 我必须使用相对路径,所以我必须执行以下操作:从项目内的文件夹开始,我必须返回直到容器目录,然后在此处获取config.properties文件。

How can I do it? 我该怎么做? (I am under Windows environment) (我在Windows环境下)

I think, in your situation, it's best to use the Classloader your Class provides. 我认为,在您的情况下,最好使用您的Class提供的Classloader

It should work like this: 它应该像这样工作:

getClass().getClassLoader().getResources("config.properties");

The only problem is, you can't compile that code in a static method like main. 唯一的问题是,您不能在像main这样的静态方法中编译该代码。 If you want to run it in a static method, you should change it to: 如果要以静态方法运行它,则应将其更改为:

Main.class.getClassLoader().getResources("config.properties");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将属性放在项目之外并同时加载属性项目属性和外部项目属性? - How can I put properties outside the project and load both properties project properties and outside project properties? Git或外部项目中的属性文件? - Properties file in Git or outside project? Java:如何访问项目文件之外的图像? - Java: How to access image outside of project file? 如何从外部项目文件夹访问hibernate.cfg.xml,config.properties和log4j.properties - How to Access hibernate.cfg.xml, config.properties, and log4j.properties from outside project folder 将休眠属性配置文件移到项目之外? - Moving hibernate properties configuration file outside of project? 在类路径之外访问属性文件 - Access a Property file outside the classpath 如何使用Java Netbeans读取项目文件夹外部的txt文件 - How to read in txt file outside of project folder with Java Netbeans 使用getResourceAsStream()在项目文件夹之外的文件的绝对路径 - Absolute path to file outside of project folder with getResourceAsStream() 如何在工作区外部的文件夹中的java类中读取属性文件? - How can I read properties file in java class located in a folder outside workspace? Java:如何将jar之外的.properties文件包含到类路径中? - Java : How to include a .properties file that is outside of jar into classpath?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM