zoukankan      html  css  js  c++  java
  • Java 获取*.properties配置文件中的内容 ,常见的两种方法

    import java.io.InputStream;
    import java.util.Enumeration;
    import java.util.List;
    import java.util.Properties;
    import java.util.ResourceBundle;
    
    import org.junit.Test;
    
    /**
     * 获取*.properties配置文件中的内容 ,常见的两种方法:
     * 
     * @author 冰雨凌風
     * 
     */
    public class ReadProperties {
        // 方法一
        @Test
        public void One() {
            // 获得资源包
            ResourceBundle bundle = ResourceBundle.getBundle("test");
            // 通过资源包拿到所有的名称
            Enumeration<String> allName = bundle.getKeys();
            // 遍历
            while (allName.hasMoreElements()) {
                // 获取每一个名称
                String name = (String) allName.nextElement();
                // 利用已得到的名称通过资源包获得相应的值
                String value = bundle.getString(name);
                System.out.println(name + "=" + value);
            }
        }
    
        // 方法二
        @Test
        public void Two() throws Exception {
            // 获得类加载器,然后把文件作为一个流获取
            InputStream in = ReadProperties.class.getClassLoader()
                    .getResourceAsStream("test.properties");
            // 创建Properties实例
            Properties prop = new Properties();
            // 将Properties和流关联
            prop.load(in);
            // 获取所有的名称
            Enumeration<?> allName = prop.propertyNames();
            // 遍历
            while (allName.hasMoreElements()) {
                // 获得每一个名称
                String name = (String) allName.nextElement();
                // 利用已得到的名称通过Properties对象获得相应的值
                String value = (String) prop.get(name);
                System.out.println(name + "=" + value);
            }
            // 关闭资源
            in.close();
        }
    }

    转:http://www.open-open.com/code/view/1425481168634
  • 相关阅读:
    python中的BeautifulSoup使用小结
    python数字前自动补零
    python列表中的所有值转换为字符串,以及列表拼接成一个字符串
    python爬虫requests过程中添加headers
    django+mysql简单总结
    python数字转换为字符串的两种方式
    python自带的IDLE如何清屏
    django模板中的自定义过滤器
    python中的requests使用小结
    在非UI线程中自制Dispatcher
  • 原文地址:https://www.cnblogs.com/xijin-wu/p/5760665.html
Copyright © 2011-2022 走看看