zoukankan      html  css  js  c++  java
  • 自定义工具类---配置文件读取方法集成类

    此类集成了一部分用于读取properties文件的数据的方法,如日期,字符串,实数型,浮点型,可自行扩展

    ConfigUtil.java

     1 package cn.jamsbwo.util;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.IOException;
     6 import java.text.ParseException;
     7 import java.text.SimpleDateFormat;
     8 import java.util.Date;
     9 import java.util.Properties;
    10 
    11 /**
    12  * 集成了读取properties文件内容的方法
    13  *     读取普通字符串
    14  *     读取文件名
    15  *     读取整形、浮点型
    16  *     读取日期类型
    17  *     配置文件名必须是config.properties
    18  * @author Administrator
    19  *
    20  */
    21 public class ConfigUtil {
    22     private static Properties config=new Properties();
    23     
    24     private ConfigUtil(){}
    25     public static Properties getConfig(){
    26         return config;
    27     }
    28     static{
    29         try {
    30             config.load(new FileInputStream(new File("config.properties")));
    31         } catch (IOException e) {
    32             e.printStackTrace();
    33         }
    34     }
    35     
    36     /**    取字符串    */
    37     public static String getString(String key){
    38         return config.getProperty(key);
    39     }
    40     
    41     /**取整数*/
    42     public static int getInt(String key){
    43         return Integer.parseInt(config.getProperty(key));
    44     }
    45     
    46     /**取浮点数double*/
    47     public static double getDouble(String key){
    48         return Double.parseDouble(config.getProperty(key));
    49     }
    50     
    51     /**    取日期(自定义格式)    */
    52     public static Date getDate(String key,String format){
    53         Date date=null;
    54         SimpleDateFormat sdf=new SimpleDateFormat(format);
    55         String dateStr=config.getProperty(key);
    56         try {
    57             date=sdf.parse(dateStr);
    58         } catch (ParseException e) {
    59             e.printStackTrace();
    60         }
    61         return date;
    62     }
    63     /**    取日期(默认1994-02-28)    */
    64     public static Date getDate(String key){
    65         return getDate(key, "yyyy-MM-dd");
    66     }    
    67 }
    View Code
  • 相关阅读:
    16进制字节和数字转换
    Vs2013 使用EF6 连接mysql数据库
    设计模式-单例模式(Singleton)
    WPF 10天修炼 第十天- WPF数据绑定
    WPF 10天修炼 第九天
    WPF 10天修炼 第八天
    WPF 10天修炼 第七天- WPF资源、样式、控件模板
    WPF 10天修炼 第六天- 系统属性和常用控件
    WPF 10天修炼 第五天- 内容控件
    WPF排版布局经验总结(干货)简短不疲倦
  • 原文地址:https://www.cnblogs.com/jamsbwo/p/4706114.html
Copyright © 2011-2022 走看看