Properties类读配置文件、修改配置文件——IO流随记7

1、Properties读文件已经修改文件

1.1Properties简介

(1)专门用于读写配置文件的集合类
配置文件的格式:
键=值
键=值
(2)注意:键值对不需要有空格,值不需要用引号一起来。默认类型是String。
(3)Properties的常见方法
load:加载配置文件的键值对到Properties对象.
list:将数据显示到指定设备.
getProperty(key):根据键获取值.
setProperty(key,value):设置键值对到Properties对象.
store:将Properties中的键值对存储到配置文件.在idea中,保存信息到配置文件,如果含有中文,会存储为unicode码.
http://tool.chinaz.com/tools/unicode.aspx unicode码查询工具.

1.2、应用案列
(1)使用Properties类完成对mysql.properties的读取.
在这里插入图片描述

public class Properties02 {

    public static void main(String [] args) throws IOException {
        //使用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 ip = properties.getProperty("ip");
        String user = properties.getProperty("user");
        String pwd = properties.getProperty("pwd");
        System.out.println(ip);
        System.out.println(user);
        System.out.println(pwd);
    }
}

在这里插入图片描述

(2)使用Properties类添加key-val 到新文件mysql2.properties中.

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @Description: TODO
 * @Author: yangyb
 * @Date:2022/1/15 18:38
 * Version: 1.0
 **/
public class Properties03 {

    public static void main(String [] args) throws IOException {
        //使用Properties类来创建配置文件,修改配置文件内容

        Properties properties = new Properties();

        //创建
        //1.如果该文件没有key就是创建
        // 2.如果该文件有key,就是修改
        properties.setProperty("charts","utf8");
        properties.setProperty("user","小明");//注意保存时,是中文的unicode码值
        properties.setProperty("pwd","admin");

        //将k-v存储在文件中即可
        properties.store(new FileOutputStream("src\\mysql2.properties"),"hello world");
        System.out.println("保存配置文件成功~");
    }
}

在这里插入图片描述
(3)使用Properties类完成对 mysql.properties的读取,并修改某个key-val.

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @Description: TODO
 * @Author: yangyb
 * @Date:2022/1/15 18:38
 * Version: 1.0
 **/
public class Properties03 {

    public static void main(String [] args) throws IOException {
        //使用Properties类来创建配置文件,修改配置文件内容
        //Properties父类是HashtableI底层就是Hashtable核心方法

        Properties properties = new Properties();

        //创建
        //1.如果该文件没有key就是创建
        // 2.如果该文件有key,就是修改
        properties.setProperty("charts","utf8");
        properties.setProperty("user","小明");//注意保存时,是中文的unicode码值
        properties.setProperty("pwd","88888888");

        //将k-v存储在文件中即可
        properties.store(new FileOutputStream("src\\mysql2.properties"),"hello world");
        System.out.println("保存配置文件成功~");
    }
}

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在VS Code中配置Git的步骤如下: 1. 确保你已经在计算机上安装了Git,并且Git的路径已经添加到系统环境变量中。 2. 打开VS Code编辑器。 3. 点击左侧的扩展图标(或按下Ctrl+Shift+X),在搜索框中输入"Git",然后选择安装"Git"扩展。 4. 安装完成后,点击左侧的源代码管理图标(或按下Ctrl+Shift+G),打开Git面板。 5. 在Git面板上方的工具栏中,点击"初始化存储库"按钮,选择一个文件夹作为你的Git仓库。 6. 点击左下角的"设置"按钮,或者使用快捷键Ctrl+逗号打开设置面板。 7. 在搜索框中输入"git.path",点击"在setting.json中编辑"。 8. 在setting.json文件中,将"git.path"的值设置为你的Git可执行文件的路径。例如:"git.path": "D:\\Software\\Git\\cmd\\git.exe"。 9. 保存并关闭setting.json文件。 现在你已经成功配置了VS Code的Git,可以使用编辑器内集成的Git功能来进行版本控制和代码管理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [VSCode配置Git的方法步骤随](https://download.csdn.net/download/weixin_38608873/14109253)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [vscode配置git](https://blog.csdn.net/qq_29304291/article/details/123021304)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值