• java --配置文件读取


             该工具类对于java项目中配置文件读取很方便~

    示例代码:

    package com.lky.util;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.Properties;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    /**
    * @ClassName: ReadConfigUtil
    * @Description: 读取配置文件工具类(properties类型和文件类型)
    * @author lky
    * @date 2015年10月26日
    * @version 1.0
     */
    public class ReadConfigUtil {
        private static Log log = LogFactory.getLog(ReadConfigUtil.class);
    
        private InputStream inputStream;
        private BufferedReader bReader;
        private Properties config;
        private String strline;
    
        public String getStrline() {
            return strline;
        }
    
        public void setStrline(String strline) {
            this.strline = strline;
        }
    
        /**
         * @describe 构造函数
         * @param fileName 文件名
         * @param flag   若为true,则表示读取properties类型的文件,否则为txt文件
         */
        public ReadConfigUtil(String fileName, boolean flag) {
            config = null;
            bReader = null;
            inputStream = ReadConfigUtil.class.getClassLoader().getResourceAsStream(fileName);
    
            try {
                if (flag) {
                    config = new Properties();
                    config.load(inputStream);
                } else {
                    bReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
                    setStrline(readFile());
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bReader != null) {
                        bReader.close();
                    }
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        public String getValue(String key) {
            return config.getProperty(key);
        }
    
        private  String readFile() {
            StringBuffer sBuffer = new StringBuffer();
    
            try {
                String line = null;
                while ((line = bReader.readLine()) != null) {
                    if (line.trim().length() > 0 && (!line.trim().startsWith("#"))) {
                        sBuffer.append(line);
                        sBuffer.append("
    ");
                    }
                }
            } catch (Exception e) {
                log.info("读文件异常!!!!");
            }
            return sBuffer.toString();
        }
    
        public static void main(String args[]) {
            System.out.println(new ReadConfigUtil("sava.txt", false).getStrline());
            System.out.println(new ReadConfigUtil("test.properties", true).getValue("ip"));
            System.out.println(new ReadConfigUtil("test.properties", true).getValue("uame"));
            System.out.println(new ReadConfigUtil("test.properties", true).getValue("password"));
        }
    }
  • 相关阅读:
    阿里HBase高可用8年“抗战”回忆录
    Service Mesh 初体验
    阿里云HBase推出普惠性高可用服务,独家支持用户的自建、混合云环境集群
    Ververica Platform-阿里巴巴全新Flink企业版揭秘
    深度 | 带领国产数据库走向世界,POLARDB底层逻辑是什么?
    AI加持的阿里云飞天大数据平台技术揭秘
    Nacos 常见问题及解决方法
    数据上云,应该选择全量抽取还是增量抽取?
    一文带你了解 Flink Forward 柏林站全部重点内容
    Oracle数据库中序列(SEQUENCE)的用法详解
  • 原文地址:https://www.cnblogs.com/dmir/p/4915711.html
Copyright © 2020-2023  润新知