您的位置:首页 > 运维架构

properties读取配置文件(配置白名单的时候)

2016-04-05 16:51 363 查看
代码说明:为了支持中文,需要使用new InputStreamReader(new FileInputStream(filePath),"utf-8")控制中文的格式
package com.my;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class Main {

    public static void main(String[] args) {
    
        try {
            String path = Main.class.getResource("/").getPath()+"/files/test.properties";
            GetAllProperties(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
    
    public static void GetAllProperties(String filePath) throws IOException {
        Properties pps = new Properties();
        //用inputStreanReader设置中文编码
        InputStreamReader is = new InputStreamReader(new FileInputStream(filePath),"utf-8");
        pps.load(is);
        Object pwd = pps.get("db.main.password");
        Object name = pps.get("db.main.username");
        Object driver = pps.get("db.main.driver");
        is.close();
        System.out.println("密码:"+pwd+"\n"+"名字:"+name+"\n"+"驱动:"+driver);

        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: