Java中的属性文件 [英] Property file in java

查看:38
本文介绍了Java中的属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的java类中读取属性文件。

I want read property file in my java class.

为此,我写了:

try {
            Properties property = new Properties();
            property .load(new FileInputStream("abc.properties"));
            String string = property.getProperty("userName");
        } catch (Exception e) {
            e.printStackTrace();
        }

我已将abc.properties文件放置在Java类所在的同一包中。但是我收到FileNotFound异常。

i have placed my abc.properties file in same package where my java class resides. But i am getting FileNotFound exception.

请让我知道如何给出属性文件的路径。

Please let me know how to give path of property file.

谢谢和问候

推荐答案

FileInputStream将在程序的工作目录中查找文件,而不是在与类相同的程序包中查找

The FileInputStream will look for the file in your program's "working directory", not in the same package as your class.

您需要使用getClass()。getResourceAsStream( abc.properties)从同一包中加载属性文件。

You need to use getClass().getResourceAsStream("abc.properties") to load your properties file from the same package.

这篇关于Java中的属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆