用Java读取属性文件

时间:2011-11-27 12:37:58

标签: java properties

我有以下代码尝试读取属性文件:

Properties prop = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();           
InputStream stream = loader.getResourceAsStream("myProp.properties");
prop.load(stream);

我在最后一行得到一个例外。具体做法是:

Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:418)
at java.util.Properties.load0(Properties.java:337)
at java.util.Properties.load(Properties.java:325)
at Assignment1.BaseStation.readPropertyFile(BaseStation.java:46)
at Assignment1.BaseStation.main(BaseStation.java:87)

感谢, 尼科斯

16 个答案:

答案 0 :(得分:79)

根据您的异常,InputStream为空,这意味着类加载器未找到您的属性文件。我猜测myProp.properties位于项目的根目录中,如果是这种情况,则需要前面的斜杠:

InputStream stream = loader.getResourceAsStream("/myProp.properties");

答案 1 :(得分:51)


您可以在此页面上找到相关信息:
http://www.mkyong.com/java/java-properties-file-examples/

Properties prop = new Properties();
try {
    //load a properties file from class path, inside static method
    prop.load(App.class.getClassLoader().getResourceAsStream("config.properties"));

    //get the property value and print it out
    System.out.println(prop.getProperty("database"));
    System.out.println(prop.getProperty("dbuser"));
    System.out.println(prop.getProperty("dbpassword"));

} 
catch (IOException ex) {
    ex.printStackTrace();
}

答案 2 :(得分:23)

您可以使用ResourceBundle类来读取属性文件。

ResourceBundle rb = ResourceBundle.getBundle("myProp.properties");

答案 3 :(得分:9)

Properties prop = new Properties();

try {
    prop.load(new FileInputStream("conf/filename.properties"));
} catch (IOException e) {
    e.printStackTrace();
}

conf/filename.properties基于项目根目录

答案 4 :(得分:7)

您无法使用关键字,例如 -

props.load(this.getClass().getResourceAsStream("myProps.properties"));

在静态环境中。

最好的办法是掌握应用程序上下文,如 -

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/META-INF/spring/app-context.xml");

然后您可以从类路径加载资源文件 -

//load a properties file from class path, inside static method
        prop.load(context.getClassLoader().getResourceAsStream("config.properties"));

这适用于静态和非静态上下文,最好的部分是此属性文件可以位于应用程序类路径中包含的任何包/文件夹中。

答案 5 :(得分:5)

您的文件应该在类路径中以com/example/foo/myProps.properties的形式提供。然后将其加载为:

props.load(this.getClass().getResourceAsStream("myProps.properties"));

答案 6 :(得分:3)

确保文件名正确并且文件实际位于类路径中。如果不是导致最后一行抛出异常的情况,getResourceAsStream()将返回null。

如果myProp.properties位于项目的根目录中,请改用/myProp.properties

答案 7 :(得分:3)

鉴于应使用上下文loader.getResourceAsStream("myPackage/myProp.properties")

前导'/'不适用于ClassLoader.getResourceAsStream(String)方法。

或者您可以使用Class.getResourceAsStream(String)方法,该方法使用'/'来确定路径是绝对路径还是相对于类位置。

<强> 示例:

myClass.class.getResourceAsStream("myProp.properties")
myClass.class.getResourceAsStream("/myPackage/myProp.properties")

答案 8 :(得分:3)

您可以使用java.io.InputStream来读取文件,如下所示:

InputStream inputStream = getClass().getClassLoader().getResourceAsStream(myProps.properties); 

答案 9 :(得分:2)

我看到这个问题很老了。如果将来有人偶然发现这一点,我认为这是一种简单的方法。 将属性文件保存在项目文件夹中。

        FileReader reader = new FileReader("Config.properties");

        Properties prop = new Properties();
        prop.load(reader);

答案 10 :(得分:1)

对于按原始顺序读取属性文件:

    File file = new File("../config/edc.properties");
    PropertiesConfiguration config = new PropertiesConfiguration();
    PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(config);
    layout.load(new InputStreamReader(new FileInputStream(file)));

    for(Object propKey : layout.getKeys()){
        PropertiesConfiguration propval =  layout.getConfiguration();
        String value = propval.getProperty((String) propKey).toString();
        out.print("Current Key:" + propkey + "Current Value:" + propval + "<br>");
    }

答案 11 :(得分:1)

当前答案中没有一个显示InputStream被关闭(这将泄漏文件描述符),和/或不处理.getResourceAsStream()在找不到资源时返回null的情况(这将导致NullPointerException的消息"inStream parameter is null"令人困惑。您需要以下内容:

String propertiesFilename = "server.properties";
Properties prop = new Properties();
try (var inputStream = getClass().getClassLoader().getResourceAsStream(propertiesFilename)) {
    if (inputStream == null) {
        throw new FileNotFoundException(propertiesFilename);
    }
    prop.load(inputStream);
} catch (IOException e) {
    throw new RuntimeException(
                "Could not read " + propertiesFilename + " resource file: " + e);
}

答案 12 :(得分:0)

如果您的属性文件路径和java类路径相同,那么您应该这样做。

例如:

  

src / myPackage / MyClass.java

     

src / myPackage / MyFile.properties

Properties prop = new Properties();
InputStream stream = MyClass.class.getResourceAsStream("MyFile.properties");
prop.load(stream);

答案 13 :(得分:0)

这里的许多答案描述了危险的方法,这些方法实例化文件输入流,但未获得对输入流的引用,以便稍后关闭该流。这导致悬挂的输入流和内存泄漏。加载属性的正确方法应类似于以下内容:

    Properties prop = new Properties();
    try(InputStream fis = new FileInputStream("myProp.properties")) {
        prop.load(fis);
    }
    catch(Exception e) {
        System.out.println("Unable to find the specified properties file");
        e.printStackTrace();
        return;
    }

请注意在try-with-resources块中实例化文件输入流。由于FileInputStream是可自动关闭的,因此在退出try-with-resources块之后它将自动关闭。如果要使用简单的try块,则必须在fis.close();块中使用finally明确将其关闭。

答案 14 :(得分:0)

如果您的config.properties不在src / main / resource目录中,而在项目的根目录中,那么您需要执行以下操作:-

Properties prop = new Properties();          
File configFile = new File(myProp.properties);
InputStream stream = new FileInputStream(configFile);
prop.load(stream);

答案 15 :(得分:-2)

指定从src开始的路径,如下所示:

src/main/resources/myprop.proper
相关问题