zoukankan      html  css  js  c++  java
  • 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"));
        }
    }
  • 相关阅读:
    过用户层HOOK思路
    Linux LVM实践
    matlab演奏卡农 Cripple Pachebel's Canon on Matlab
    rman备份恢复总结
    郁金香VC外挂教程(全) 翻录版 免Key(精品教程)
    C# string 中的 @ 作用处理\等字符
    (抓)2分法通用存储过程分页(top max模式)版本(性能相对之前的not in版本极大提高)
    怎样应用OracleParameter怎样写like查询语句?
    (转)DirectoryEntry的使用
    解决模式对话框和window.open打开新页面Session会丢失问题
  • 原文地址:https://www.cnblogs.com/dmir/p/4915711.html
Copyright © 2011-2022 走看看