properties类

public class FileInformation {
    public static void main(String[] args) {

    }
    @Test
    public void Properties02() throws Exception {
        //使用Properties类来读取mysql.properties文件
        //1.创建Properties对象
        Properties properties = new Properties();
        //2.加载指定配置文件
        properties.load(new FileReader("src\\mysql.properties"));
        //3.把k-v显示控制台
        properties.list(System.out);
        //4.根据key获取对应的值
        String  user = properties.getProperty("user");
        String  pwd = properties.getProperty("pwd");
        System.out.println("用户名="+user);
        System.out.println("密码="+pwd);
        //5.修改
        properties.setProperty("charest","utf8");
        properties.setProperty("user","马青松");
        properties.setProperty("pwd","asbf");
        //将k-v存储文件中即可
        properties.store(new FileOutputStream("src\\mysql2.properties"),null);
        System.out.println("保存成功");
    }
}

 

posted @ 2022-08-01 09:48  贝勒爷ma  阅读(18)  评论(0)    收藏  举报