如何在Netbeans(JAVA)上从另一个包打开属性文件

如何在Netbeans(JAVA)上从另一个包打开属性文件,java,netbeans,nullpointerexception,fileinputstream,Java,Netbeans,Nullpointerexception,Fileinputstream,我有一个bean,它需要从属性文件中获取一些参数,但我找不到它(java.lang.NullPointerException)来打开它。我的bean在extra.beans包中,而属性文件在extra.dao包中。我正在努力 file = new FileInputStream("database.properties"); prop.load(file); 我尝试过任何可能的组合,但我找不到它。我正在使用Netbeans 7.4。如何打开它?指定完整路径。它应该可以工作。指定完整路径。它应该

我有一个bean,它需要从属性文件中获取一些参数,但我找不到它(java.lang.NullPointerException)来打开它。我的bean在extra.beans包中,而属性文件在extra.dao包中。我正在努力

file = new FileInputStream("database.properties");
prop.load(file);

我尝试过任何可能的组合,但我找不到它。我正在使用Netbeans 7.4。如何打开它?

指定完整路径。它应该可以工作。

指定完整路径。它应该可以工作。

如果要将属性文件加载到
属性
对象中,请尝试以下操作:

Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("../dao/database.properties"));

我不知道您的完整包结构,但使用这种方法并将完整路径放入属性文件也会起作用,即:
/extra/dao/database.properties

如果要将属性文件加载到
属性
对象中,请尝试以下操作:

Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("../dao/database.properties"));
file = getClass().getClassLoader().getResourceAsStream("extra/dao/database.properties") ;
prop.load(file);

我不知道您的完整包结构,但使用这种方法并将完整路径放入属性文件也会起作用,即:
/extra/dao/database.properties

您可以使用资源包来实现这一点

file = getClass().getClassLoader().getResourceAsStream("extra/dao/database.properties") ;
prop.load(file);
ResourceBundle resBundle =  ResourceBundle.getBundle("PropertyFileName");  // without extention 
String name=  resBundle.getString("Required Attribute");  // example username

您可以为此使用资源包

ResourceBundle resBundle =  ResourceBundle.getBundle("PropertyFileName");  // without extention 
String name=  resBundle.getString("Required Attribute");  // example username