配置文件读取工具类--PropertiesUtil

/**
 * 属性工具类
 * @author admin
 * 参考:https://www.cnblogs.com/doudouxiaoye/p/5693454.html
 */
public class PropertiesUtil {
    private static Properties pro=null;
    private static Map<String,Object> map=new HashMap<String, Object>();
    
    //静态块中加载
    static {
        //读取多个配置文件
        init("b.properties");//类路径下直接使用文件名,文件加载方式要全路径名
//        init("fileName2");
    }
    
    //读取配置文件操作
    private static void init(String fileName) {
        try {
            pro = new Properties();
            //文件方式
//            pro.load(new FileInputStream(new File(fileName)));
            //类加载器方式
            pro.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName));
            
          //可遍历properties的key和value
            //方式一  key(string)集合
           /* for (String key : pro.stringPropertyNames()) {
                System.out.println(key + "=" + pro.getProperty(key));
                //存入map中
                map.put(key, pro.getProperty(key));//string
                map.put(key, pro.get(key));//对象
            }*/
            
          //方式二  key(对象)集合
           /* Set<Object> keys=pro.keySet();
            for (Object key : keys) {
                System.out.println(key + "=" + pro.get(key));
                //存入map中
                map.put(key.toString(), pro.get(key));
            }*/
            
          //方式三  键值对集合(全面)
            Set<Map.Entry<Object, Object>> entrySet = pro.entrySet();//返回的属性键值对实体
            for (Map.Entry<Object, Object> entry : entrySet) {
                System.out.println(entry.getKey() + "=" + entry.getValue());
              //存入map中
                map.put(entry.getKey().toString(), entry.getValue());
            }
            //或迭代器方式
            Iterator<Entry<Object, Object>> it=entrySet.iterator();
            while(it.hasNext()) {
                Map.Entry<Object, Object> entry =it.next();
                System.out.println(entry.getKey() + "=" + entry.getValue());
                //存入map中
                map.put(entry.getKey().toString(), entry.getValue());
            }
            
          //方式三  Enumeration(传统接口)
           /* Enumeration<?> e = pro.propertyNames();
            while (e.hasMoreElements()) {
                String key = (String) e.nextElement();
                String value = pro.getProperty(key);
                System.out.println(key + "=" + value);
              //存入map中
                map.put(key, value);
            }*/

            
          //保存属性到b.properties文件
//            FileOutputStream oFile = new FileOutputStream("b.properties", true);//true表示追加打开
//            pro.setProperty("phone", "10086");
//            pro.store(oFile, "The New properties file");
//            oFile.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 简单调用方式
     */
    //方式1:静态方法调用
    public static String getValue(String key) {
        return pro.getProperty(key);
    }

    //方式2:静态常量调用
    public static final String FILE_NAME=pro.getProperty("fileName");
    
    public static void main(String[] args) {
        System.out.println("调用方式1 "+PropertiesUtil.pro.getProperty("fileName"));
        System.out.println("调用方式2 "+PropertiesUtil.FILE_NAME);
        System.out.println("调用方式3 "+PropertiesUtil.getValue("fileName"));
    }
}
View Code
/**
 * 属性工具类
 * @author admin
 * 参考:https://www.cnblogs.com/doudouxiaoye/p/5693454.html
 */
public class PropertiesUtil {
    private static Properties pro=null;
    private static Map<String,Object> map=new HashMap<String, Object>();
    
    //静态块中加载
    static {
        //读取多个配置文件
        init("b.properties");//类路径下直接使用文件名,文件加载方式要全路径名
//        init("fileName2");
    }
    
    //读取配置文件操作
    private static void init(String fileName) {
        try {
            pro = new Properties();
            //文件方式
//            pro.load(new FileInputStream(new File(fileName)));
            //类加载器方式
            pro.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName));
            
          //可遍历properties的key和value
            //方式一  key(string)集合
           /* for (String key : pro.stringPropertyNames()) {
                System.out.println(key + "=" + pro.getProperty(key));
                //存入map中
                map.put(key, pro.getProperty(key));//string
                map.put(key, pro.get(key));//对象
            }*/
            
          //方式二  key(对象)集合
           /* Set<Object> keys=pro.keySet();
            for (Object key : keys) {
                System.out.println(key + "=" + pro.get(key));
                //存入map中
                map.put(key.toString(), pro.get(key));
            }*/
            
          //方式三  键值对集合(全面)
            Set<Map.Entry<Object, Object>> entrySet = pro.entrySet();//返回的属性键值对实体
            for (Map.Entry<Object, Object> entry : entrySet) {
                System.out.println(entry.getKey() + "=" + entry.getValue());
              //存入map中
                map.put(entry.getKey().toString(), entry.getValue());
            }
            //或迭代器方式
            Iterator<Entry<Object, Object>> it=entrySet.iterator();
            while(it.hasNext()) {
                Map.Entry<Object, Object> entry =it.next();
                System.out.println(entry.getKey() + "=" + entry.getValue());
                //存入map中
                map.put(entry.getKey().toString(), entry.getValue());
            }
            
          //方式三  Enumeration(传统接口)
           /* Enumeration<?> e = pro.propertyNames();
            while (e.hasMoreElements()) {
                String key = (String) e.nextElement();
                String value = pro.getProperty(key);
                System.out.println(key + "=" + value);
              //存入map中
                map.put(key, value);
            }*/

            
          //保存属性到b.properties文件
//            FileOutputStream oFile = new FileOutputStream("b.properties", true);//true表示追加打开
//            pro.setProperty("phone", "10086");
//            pro.store(oFile, "The New properties file");
//            oFile.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 简单调用方式
     */
    //方式1:静态方法调用
    public static String getValue(String key) {
        return pro.getProperty(key);
    }

    //方式2:静态常量调用
    public static final String FILE_NAME=pro.getProperty("fileName");
    
    public static void main(String[] args) {
        System.out.println("调用方式1 "+PropertiesUtil.pro.getProperty("fileName"));
        System.out.println("调用方式2 "+PropertiesUtil.FILE_NAME);
        System.out.println("调用方式3 "+PropertiesUtil.getValue("fileName"));
    }
}

猜你喜欢

转载自www.cnblogs.com/cslj2013/p/10387190.html