Properties类的简单运用

当需要用到 .properties 配置文件时,我们往往会用到Properties类创造途径来访问配置文件的内容.

示例:现在我创建了一个后缀为.properties的文件.(创建方式同创建记事本,将后缀改为.properties即可)

注意:.properties文件仅当放在项目目录下才有效

 

程序中,为了访问这个文件,我需要写下面这些代码创建与文件的连接:

1:新建一个Properties 类的实例

Properties userInfomation = new Properties();

2:创建文件流并将流连接到实例上
InputStream is;
OutputStream os;
is = new FileInputStream("userInfo.properties");
os = new FileOutputStream("userInfo.properties", true);

3:读文件
userInfomation.load(is);

userInfomation.containsKey(userName);

userInfomation.getProperty("username");

4:写文件
// 将用户名和密码存储到内存中
userInfomation.setProperty(userName, userPassword);
// 将用户名和密码保存到文件中
userInfomation.store(os, null);

 

posted @ 2017-04-06 01:54    阅读(147)  评论(0编辑  收藏  举报