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

在这里插入图片描述

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中进行如下设置。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_46112274/article/details/124721305
今日推荐