1 packagecom;2
3 importjava.io.BufferedInputStream;4 importjava.io.File;5 importjava.io.FileInputStream;6 importjava.io.FileNotFoundException;7 importjava.io.IOException;8 importjava.io.InputStream;9 importjava.io.InputStreamReader;10 importjava.util.Properties;11 importjava.util.PropertyResourceBundle;12 importjava.util.ResourceBundle;13
14 public classLoadPropertiesFile {15 private static final String DEFAULT_ENCODING = "UTF-8";16
17 /**
18 * 使用java.util.Properties类的load()方法加载properties文件19 */
20 private static voidMethod1() {21 try{22 //获取文件流(方法1或2均可)
23 InputStream inputStream = new BufferedInputStream(new FileInputStream(new File("src/main/resources/demo/jdbc.properties"))); //方法124 //InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties");//方法2
25 Properties prop = newProperties();26
27 prop.load(new InputStreamReader(inputStream, DEFAULT_ENCODING)); //加载格式化后的流
28
29 String driverClassName = prop.getProperty("driverClassName");30
31 System.out.println("Method1: " +driverClassName);32
33 } catch(FileNotFoundException e) {34 System.out.println("properties文件路径有误!");35 e.printStackTrace();36 } catch(IOException e) {37 e.printStackTrace();38 }39 }40
41 /**
42 * 使用class变量的getResourceAsStream()方法43 * 注意:getResourceAsStream()方法的参数路径/包路径+properties文件名+.后缀44 */
45 public static voidMethod2() {46 try{47 InputStream inputStream = LoadPropertiesFile.class.getResourceAsStream("/demo/jdbc.properties");48
49 Properties prop = newProperties();50 prop.load(inputStream);51
52 String driverClassName = prop.getProperty("driverClassName");53
54 System.out.println("Method2: " +driverClassName);55 } catch(IOException e) {56 e.printStackTrace();57 }58 }59
60 /**
61 * 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法62 * 注意:getResourceAsStream(name)方法的参数必须是包路径+文件名+.后缀63 */
64 public static voidMethod3() {65 try{66 InputStream inputStream = LoadPropertiesFile.class.getClassLoader().getResourceAsStream("demo/jdbc.properties");67
68 Properties prop = newProperties();69 prop.load(inputStream);70
71 String driverClassName = prop.getProperty("driverClassName");72
73 System.out.println("Method3: " +driverClassName);74 } catch(IOException e) {75 e.printStackTrace();76 }77 }78
79 /**
80 * 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法81 * getSystemResourceAsStream()方法的参数必须是包路径+文件名+.后缀82 */
83 public static voidMethod4() {84 try{85 InputStream inputStream = ClassLoader.getSystemResourceAsStream("demo/jdbc.properties");86
87 Properties prop = newProperties();88 prop.load(inputStream);89
90 String driverClassName = prop.getProperty("driverClassName");91
92 System.out.println("Method4: " +driverClassName);93 } catch(IOException e) {94 e.printStackTrace();95 }96 }97
98 /**
99 * 使用java.util.ResourceBundle类的getBundle()方法100 * 注意:注意:这个getBundle()方法的参数相对同目录路径,并去掉.properties后缀,否则将抛异常101 */
102 public static voidMethod5() {103 ResourceBundle resource = ResourceBundle.getBundle("demo");104 String driverClassName = resource.getString("driverClassName");105
106 System.out.println("Method5: " +driverClassName);107 }108
109 /**
110 * 使用java.util.PropertyResourceBundle类的构造函数111 */
112 public static voidMethod6(){113 ResourceBundle resource;114 try{115 InputStream inputStream = new BufferedInputStream(new FileInputStream(new File("src/main/resources/demo/jdbc.properties")));116 resource = newPropertyResourceBundle(inputStream);117
118 String driverClassName = resource.getString("driverClassName");119 System.out.println("Method6: " +driverClassName);120
121 } catch(FileNotFoundException e) {122 e.printStackTrace();123 } catch(IOException e) {124 e.printStackTrace();125 }126 }127
128 public static voidmain(String[] args) {129 Method1();130 Method2();131 Method3();132 Method4();133 Method5();134 Method6();135 }136 }