zoukankan      html  css  js  c++  java
  • java读取配置文件常用的四种方式

    配置文件 
    放置在src下面 obj.properties

    className=com.store.order.dao.impl.OrderDaoImpl


    方式一

    @Test
        public void test1() throws Exception{
            //文件放在src下面.eclipse会自动拷贝一份到bin目录下,或者build/classes下面,
            InputStream is = Class.forName("com.store.test.test").getClassLoader().getResourceAsStream("obj.properties");
            Properties properties = new Properties();
            properties.load(is);
            String className = properties.getProperty("className");
            System.out.println(className);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    方式二

        /**
         * 不推荐使用,文件位置被固定了,只能在本机使用,我们把class文件拷贝给别人就不能使用了
         * @throws Exception
         */
        @Test
        public void test2() throws Exception{
            FileInputStream fis = new FileInputStream("D:\workspaces\store_v1.0\src\obj.properties");
            Properties properties = new Properties();
            properties.load(fis);
            String className = properties.getProperty("className");
            System.out.println(className);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    方式三

        @Test
        public void test3() throws Exception{
            //读取配置文件,只能读取默认后缀名为properties(不写)
            //如果有包,写法是com.store.obj
            //细节:他只能读取类路路径下的文件,不能写入
            ResourceBundle bundle = ResourceBundle.getBundle("obj"); 
            String className = bundle.getString("className"); //通过键获取值
            System.out.println(className);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    方式四

    /** 
     * Spring 提供的 PropertiesLoaderUtils 允许您直接通过基于类路径的文件地址加载属性资源 
     * 最大的好处就是:实时加载配置文件,修改后立即生效,不必重启 
     */  
    private static void springUtil(){  
        Properties props = new Properties();  
        while(true){  
            try {  
                props=PropertiesLoaderUtils.loadAllProperties("message.properties");  
                for(Object key:props.keySet()){  
                    System.out.print(key+":");  
                    System.out.println(props.get(key));  
                }  
            } catch (IOException e) {  
                System.out.println(e.getMessage());  
            }  
    
            try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}  
        }  
    }  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    image/pjpeg和image/jpeg问题
    windows server 2003 服务器中 HTTP 错误401.1 未经授权:访问由于凭据无效被拒绝
    解决了界面上菜单项跑到其它AE控件后面的问题(java)
    清除地图中的所有图层和FileFilter的使用
    设置pagelayoutControl控件显示滚动条
    pagelayoutControl中添加图元(VB)
    添加和删除字段(vb)
    用代码实现toolbar弹出ButtonMenus(VB)
    pageLayoutControl与Mapcontrol同步(VB)
    C++ Builder XE2随意学习 (1)
  • 原文地址:https://www.cnblogs.com/edgedance/p/6979665.html
Copyright © 2011-2022 走看看