JavaSE-集合-Properties(常用语于处理配置文件)

该博客介绍了如何使用Java Properties类读取配置文件,通过FileInputStream加载jdbc.properties文件并获取其中的name和password。同时提到了在IDEA中处理中文显示为Unicode编码的问题,需要进行相应的设置确保正常显示。
摘要由CSDN通过智能技术生成

在这里插入图片描述

1、PropertiesTest

  • Properties:常用来处理配置文件,key和value都是
  • Ctrl+shift+T:生成try/catch代码的快捷键
public class PropertiesTest {
    //Properties:常用来处理配置文件,key和value都是String类型
    //Ctrl+shift+T:选择一部分代码,生成部分代码的快捷键
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        try {
            Properties properties = new Properties();

            fileInputStream = new FileInputStream("jdbc.properties");
            properties.load(fileInputStream);//加载流对应的文件

            String name = properties.getProperty("name");
            String password = properties.getProperty("password");

            System.out.println("name="+name+",password="+password);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileInputStream !=null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

在这里插入图片描述

2、jdbc.properties

在这里插入图片描述
如果上边jdbc.properties文件中中文显示为unicode编码,则需要在idea中进行如下设置。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值