简体   繁体   English

在conf文件夹中读取属性文件

[英]Reading Properties file inside conf folder

I have a properties file which is located under conf folder. 我有一个位于conf文件夹下的属性文件。 conf folder is under the project root directory. conf文件夹位于项目根目录下。 I am using the following code. 我正在使用以下代码。

public class PropertiesTest {
 public static void main(String[] args) {
    InputStream inputStream = PropertiesTest.class
            .getResourceAsStream("/conf/sampleprop.conf");
    Properties prop = new Properties();
    try {
        prop.load(inputStream);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println(prop.getProperty("TEST"));
}
}

But I get nullpointer exception. 但是我得到了nullpointer异常。

I have tried using 我尝试使用

InputStream inputStream = PropertiesTest.class
        .getResourceAsStream("./conf/sampleprop.conf");

and

InputStream inputStream = PropertiesTest.class
        .getResourceAsStream("conf/sampleprop.conf");

But all result in nullpointer exception. 但是所有这些都会导致nullpointer异常。 Can anyone please help. 谁能帮忙。 Thanks in advance 提前致谢

Try to recover your working directory first: 尝试首先恢复您的工作目录:

String workingDir = System.getProperty("user.dir");
System.out.println("Current working dir: " + workingDir);

and then is simple: 然后很简单:

Properties propertiesFile = new Properties();
propertiesFile.load(new FileInputStream(workingDir+ "/yourFilePath"));
    String first= propertiesFile.getProperty("myprop.first");

Regards, fabio 问候,法比奥

The getResourceAsStream() method tries to locate and load the resource using the ClassLoader of the class it is called on. getResourceAsStream()方法尝试使用被调用的类的ClassLoader定位和加载资源。 Ideally it can locate the files only the class folders .. Rather you could use FileInputStream with relative path. 理想情况下,它只能定位类文件夹中的文件。而是可以将FileInputStream与相对路径一起使用。

EDIT 编辑

if the conf folder is under src, then you still be able to access with getResourceAsStream() 如果conf文件夹位于src下,那么您仍然可以使用getResourceAsStream()访问

 InputStream inputStream = Test.class
                    .getResourceAsStream("../conf/sampleprop.conf");

the path would be relative to the class from you invoke getRes.. method. 路径将相对于您调用getRes..方法的类。

If not 如果不

 try {
                FileInputStream fis = new FileInputStream("conf/sampleprop.conf");
                Properties prop = new Properties();
                prop.load(fis);
                System.out.println(prop.getProperty("TEST"));
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

NOTE: this will only work if it is Stand alone application/in eclipse. 注意:仅当它是独立应用程序/日食时,这才起作用。 This will not work if its web based (as the root will be Tomcat/bin, for eg) 如果它是基于Web的,这将不起作用(例如,根目录将为Tomcat / bin)

I would suggest to copy the configuration file at designated place, then you can acess at ease. 我建议将配置文件复制到指定位置,这样您就可以放心了。 At certain extent 'System.getProperty("user.dir")' can be used if you are always copying the file 'tomcat` root or application root. 如果您始终要复制文件“ tomcat”根目录或应用程序根目录,则可以在某种程度上使用“ System.getProperty(“ user.dir”)”。 But if the files to be used by external party, ideal to copy in a configurable folder (C:\\appconf) 但是,如果文件要由外部方使用,则最好将其复制到可配置的文件夹(C:\\ appconf)中

Your code works like a charm! 您的代码就像一个魅力! But you might have to add the project root dir to your classpath. 但是您可能必须将项目根目录添加到类路径中。

If you work with Maven, place your configuration in src/main/resources/conf/sampleprop.conf 如果使用Maven,请将配置放在src / main / resources / conf / sampleprop.conf中

When invoking java directly add the project root dir with the java -classpath parameter. 调用Java时,直接使用java -classpath参数添加项目根目录。 Something like: 就像是:

java -classpath /my/classes/dir:/my/project/root/dir my.Main

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM