java中读取Properties文件
java 中读取Properties 文件
关键字: java properties 在java 中,可以用
java 代码
- Properties p = new Properties ();
- InputStream is = this .getClass().getResourceAsStream(propertiesFileName);
- p.load(is);
- is.close;
来读取。
如果properties 文件是在classpath的根目录下,如test.properties 在src目录下,则可以这样读取:
- InputStream is = this .getClass().getResourceAsStream(" test.properties " );
如果不在要目录下,则要加上相对于classpath要目录的路径。如在src目录下,有一个包com.myapp.resources,则可以如下读取:
- InputStream is = this .getClass().getResourceAsStream( "/com/myapp/resource/test.properties " );
- 在读取后最后调用一次trim(),以免多了个空格就导致取不到相应的property