web项目读写src目录下的.properties文件

来源:互联网 发布:易语言微信加好友源码 编辑:程序博客网 时间:2024/05/17 08:40

针对放在WebRoot目录下的properties文件

1.读properties文件

Properties pro = new Properties();
pro.load(ConfigServlet.class.getResourceAsStream("/config.properties"));

pro.getProperty("key");


2.写properties

Properties pro = new Properties();

pro.setProperty("key", "value");

FileOutputStream fos = new FileOutputStream(this.class.getResource("/config.properties").getPath());

pro.store(fos, "comments");


针对放置于src目录下的properties文件

src目录下的properties文件,编译后会放置于classes目录下,读取的时候可以使用

1. XX.class.getResourceAsStream("/config.properties");

2. new FileInputSteam("/config.properties")

这两种方式。


写入的时候使用new FileOutputStream("/config.properties")方式获取文件流。

0 0