当需要用到 .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);

 

相关文章:

  • 2022-12-23
  • 2021-11-21
  • 2021-08-27
  • 2021-10-15
  • 2022-12-23
  • 2022-01-13
  • 2021-12-01
  • 2022-12-23
猜你喜欢
  • 2021-09-16
  • 2021-11-13
  • 2021-06-07
  • 2022-12-23
  • 2021-12-13
  • 2021-06-03
  • 2022-12-23
相关资源
相似解决方案