zoukankan      html  css  js  c++  java
  • java中读取Properties文件----五种方式

    Mark:

    读取properties对于java编程来说很重要,所以小本本赶紧记起来吧。-----读取方式参考了其他博客文章,望大佬莫见怪

    public class TestProperties {
        public String path1="com/pengzhen/test/jdbc.properties";
        public String path2="/com/pengzhen/test/jdbc.properties";
        public String path3="src/com/pengzhen/test/jdbc.properties";
        public String path4="com/pengzhen/test/jdbc";
        public String path5="src/com/pengzhen/test/jdbc.properties";    
        /*第一种采用的是类加载器*/
        public void WayOne(){
            ClassLoader loader=this.getClass().getClassLoader();
            InputStream  is=loader.getResourceAsStream(path1);
            Properties properties=new Properties();
            try {
                properties.load(is);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String url=properties.getProperty("jdbc.url");
            String username=properties.getProperty("jdbc.username");
            System.out.println(url+"||"+username);
        }
        /*第二种采用的是运行类本身的方法*/
        public void WayTwo(){
            InputStream is=this.getClass().getResourceAsStream(path2);
            Properties properties=new Properties();
            try {
                properties.load(is);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String url=properties.getProperty("jdbc.url");
            String username=properties.getProperty("jdbc.username");
            System.out.println(url+"||"+username);
        }
        /*第三种采用的是文件流的形式,并用Properties.load方法加载该流*/
        public void WayThree(){
            InputStream is = null;
            try {
                is = new BufferedInputStream(new FileInputStream(new File("src/com/pengzhen/test/jdbc.properties")));
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            Properties properties=new Properties();
            try {
                properties.load(is);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String url=properties.getProperty("jdbc.url");
            String username=properties.getProperty("jdbc.username");
            System.out.println(url+"||"+username);
        }
        /*第四种采用的是ResourceBundle的getBundle方法*/
        public void WayFour(){
            ResourceBundle rBundle=ResourceBundle.getBundle(path4);
            String url=rBundle.getString("jdbc.url");
            String username=rBundle.getString("jdbc.username");
            System.out.println(url+"||"+username);
        }
        /*第五种采用的是PropertyResourceBundle类的构造方法*/
        public void WayFive(){
            InputStream is = null;
            try {
                is = new BufferedInputStream(new FileInputStream(path5));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            PropertyResourceBundle propertyResourceBundle = null;
            try {
                propertyResourceBundle = new PropertyResourceBundle(is);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String url=propertyResourceBundle.getString("jdbc.url");
            String username=propertyResourceBundle.getString("jdbc.username");
            System.out.println(url+"||"+username);
        }
        public static void main(String[] args) {
            TestProperties properties=new TestProperties();
            properties.WayOne();
            properties.WayTwo();
            properties.WayThree();
            properties.WayFour();
            properties.WayFive();
        }    
    }
  • 相关阅读:
    Android使得Fragment 切换时不重新实例化
    根据设备宽高设置View的大小
    解决ScrollView中包含ListView,导致ListView显示不全
    android 学习随笔二十三(动画:Fragment )
    android 学习随笔二十二(小结)
    android 学习随笔二十一(内容提供者 )
    android 学习随笔二十(多媒体编程 )
    android 学习随笔十九(对话框、样式、主题、国际化 )
    android 学习随笔十八(广播与服务 )
    android 学习随笔十七(服务 )
  • 原文地址:https://www.cnblogs.com/Pzhenzhen/p/9761049.html
Copyright © 2011-2022 走看看