关于读取Property文件里面的内容

  读取java web 项目下的文件的路径,我们一般有几种读法。如果在同一个包路径下,可直接用InputStream inputStream  =getClass().getResourceAsStream("path.properties"); 来读取this可去可不去。如果是在resource路径的话,可以以

InputStream =inputStreamthis.getClass().getResourceAsStream("/path.properties");形式来读取,this不能去掉。

然后 Properties p  =   new  Properties();    

 try   {    

 p.load(inputStream);    

  }   catch  (IOException e1)  {    

 e1.printStackTrace();    

 }    

//返回rsa路径

return  p.getProperty("file.rsapath" );

还有一种办法,就是spring的注入方法,<bean

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="order" value="1" />

<property name="ignoreUnresolvablePlaceholders" value="true" />

<property name="locations">

<list>

<value>classpath:path.properties</value

</list>

</property>

</bean>

 然后在java中用@Value("${file.rsapath}")来读取文件中对应的值

猜你喜欢

转载自201609193834.iteye.com/blog/2344984