加载properties文件的6种方法

来源:互联网 发布:软件项目标书 编辑:程序博客网 时间:2024/05/18 20:46
总结

在普通的Java项目中,所有的方法都能成功 


在开发项目中,能成功的方法只有第一种,第三种和第六种

其中第一种和第六种方法需要的入参是properties文件的全类名

第三种方法在开发项目中区别于普通的Java项目,需要的入参只能为properties文件名,不含包路径


package util;    import java.io.BufferedInputStream;  import java.io.FileInputStream;  import java.io.FileNotFoundException;  import java.io.IOException;  import java.io.InputStream;  import java.util.Properties;  import java.util.PropertyResourceBundle;  import java.util.ResourceBundle;    import org.junit.Test;    /**方法   加载properties文件的6种基本方式     *   * java加载properties文件的方式主要分为两大类:  * 第一类,首先获得文件的输入流,再通过import java.util.Properties类中的load(InputStream in)方法加载properties对象,    通过Properties对象来操作文件内容  * 第二类,首先通过import java.util.ResourceBundle类的getBundle(String baseName)方法加载properties文件, 然后ResourceBundle对象来操做properties文件内容  *   * 其中最重要的就是每种方式加载文件时,文件的路径需要按照方法的定义的格式来加载,否则会抛出各种异常,比如空指针异常  *   * @author 方轩灵动  * @version 2017-10-24  */  public class TestProperties {      private static String Path_properties = "src/util/db.properties";//全类名      private static String Url = null;      private static String DriverClassName = null;      private static String User = null;      private static String Password = null;          //---类别1    首先获得文件输入流   通过Properties对象来操作文件内容 ---开始        /**方法   第1种方法       重要,全类名  在项目中成功      * FileInputStream读取全类名      */      @Test      public void loadProperties1(){          //获得properties          Properties property = new Properties();//流文件            InputStream ins = null;          try {              ins = new FileInputStream(Path_properties);          } catch (FileNotFoundException e) {              e.printStackTrace();          }            //InputStream in2 = TestProperties.class.getClassLoader().getResourceAsStream("db.properties");            try {                  property.load(ins);  //              property2.load(in2);//in2加载错误,获得的是null          } catch (IOException e1) {              System.out.println("Properties加载失败");              e1.printStackTrace();          }            printProperties(property);      }        /**方法   第2种方法       重要,加载properties文件的类 在项目中失败      *       * 使用class变量的getResourceAsStream()方法       * 注意,getResourceAsStream()方法的参数按格式写到   包路径+properties文件名+.后缀, 根目录 有前缀 / 符号      */      @Test      public void loadProperties2(){          InputStream ins = TestProperties.class.getResourceAsStream("/util/db.properties");            Properties property = new Properties();            try {                property.load(ins);            } catch (IOException e) {                e.printStackTrace();            }              printProperties(property);      }        /**方法   第3种方法       重要,加载properties文件的类 在项目中成功,但不能添加包路径,直接传参properties文件名才能成功加载,例如src/main/resources/db.properties,传参db.properties      *       * 对比方法2,TestProperties.class.后面还多了.getClassLoader()方法,根目录 没有前缀 / 符号      *       * 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法      * getResourceAsStream(name)方法的参数必须是    包路径+properties文件名+.后缀,包路径没有前缀 / 符号      * 否则会报空指针异常      */      @Test      public void loadProperties3(){          InputStream ins = TestProperties.class.getClassLoader().getResourceAsStream("util/db.properties");            Properties property = new Properties();            try {                property.load(ins);            } catch (IOException e) {                e.printStackTrace();            }              printProperties(property);      }        /**方法   第4种方法       使用类加载器ClassLoader   在项目中失败      *       * 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法       * getSystemResourceAsStream()方法的参数格式也是有固定要求的,  包路径+properties文件名+.后缀,根目录 没有前缀 / 符号      */      @Test      public void loadProperties4(){          InputStream ins = ClassLoader.getSystemResourceAsStream("util/db.properties");            Properties property = new Properties();          try {              property.load(ins);          } catch (IOException e) {              e.printStackTrace();          }            printProperties(property);      }        //---类别1    首先获得文件输入流   通过Properties对象来操作文件内容 ---结束        //---类别2    首先获得properties文件    通过ResourceBundle对象来操做properties文件内容---开始        /**方法   第5种方法       例如文件db.properties,只需要文件名db,不需要拓展名.properties        在项目中失败      *       * 使用java.util.ResourceBundle类的getBundle()方法       * 注意:这个getBundle()方法的参数只能写成    包路径+properties文件名,根目录 没有前缀 / 符号      * 否则将抛异常         */      @Test      public void loadProperties5(){          ResourceBundle rb = ResourceBundle.getBundle("util/db");             //rb.getString()方法,参数是properties文件的各属性名          System.out.println(rb.getString("jdbc.driverClassName"));          System.out.println(rb.getString("jdbc.url"));          System.out.println(rb.getString("jdbc.username"));          System.out.println(rb.getString("jdbc.password"));      }        /**方法   第6种方法       重要,properties的全类名   在项目中成功      *       *  使用java.util.PropertyResourceBundle类的构造函数          */      @Test      public void loadProperties6(){           InputStream ins = null;           ResourceBundle rb = null;               try {                    ins = new BufferedInputStream(new FileInputStream(Path_properties));                     rb = new PropertyResourceBundle(ins);                } catch (FileNotFoundException e) {                    e.printStackTrace();                } catch (IOException e) {                    e.printStackTrace();                }              //rb.getString()方法,参数是properties文件的各属性名          System.out.println(rb.getString("jdbc.driverClassName"));          System.out.println(rb.getString("jdbc.url"));          System.out.println(rb.getString("jdbc.username"));          System.out.println(rb.getString("jdbc.password"));      }        //---类别2    首先获得properties文件    通过ResourceBundle对象来操做properties文件内容---结束        /**方法   输出  Properties 的属性      * @param property      */      public void printProperties(Properties property){          //连接属性          DriverClassName = property.getProperty("jdbc.driverClassName");          Url = property.getProperty("jdbc.url");          User = property.getProperty("jdbc.username");          Password = property.getProperty("jdbc.password");            System.out.println(DriverClassName+"\n"+Url+"\n"+User+"\n"+Password);          System.out.println();      }    }