java加载properties文件的几种方式
大致分为两种方式,一是获取文件流,然后通过工具类加载,二是直接通过工具类加载;一下是几种加载方法的具体示例代码:
1 package com; 2 3 import java.io.BufferedInputStream; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.io.InputStreamReader; 10 import java.util.Properties; 11 import java.util.PropertyResourceBundle; 12 import java.util.ResourceBundle; 13 14 public class LoadPropertiesFile { 15 private static final String DEFAULT_ENCODING = "UTF-8"; 16 17 /** 18 * 使用java.util.Properties类的load()方法加载properties文件 19 */ 20 private static void Method1() { 21 try { 22 // 获取文件流(方法1或2均可) 23 InputStream inputStream = new BufferedInputStream(new FileInputStream(new File("src/main/resources/demo/jdbc.properties"))); //方法1 24 // InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties"); //方法2 25 Properties prop = new Properties(); 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 void Method2() { 46 try { 47 InputStream inputStream = LoadPropertiesFile.class.getResourceAsStream("/demo/jdbc.properties"); 48 49 Properties prop = new Properties(); 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 void Method3() { 65 try { 66 InputStream inputStream = LoadPropertiesFile.class.getClassLoader().getResourceAsStream("demo/jdbc.properties"); 67 68 Properties prop = new Properties(); 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 void Method4() { 84 try { 85 InputStream inputStream = ClassLoader.getSystemResourceAsStream("demo/jdbc.properties"); 86 87 Properties prop = new Properties(); 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 void Method5() { 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 void Method6(){ 113 ResourceBundle resource; 114 try { 115 InputStream inputStream = new BufferedInputStream(new FileInputStream(new File("src/main/resources/demo/jdbc.properties"))); 116 resource = new PropertyResourceBundle(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 void main(String[] args) { 129 Method1(); 130 Method2(); 131 Method3(); 132 Method4(); 133 Method5(); 134 Method6(); 135 } 136 }
说明:
其中1,2,3,4都是先获得文件的输入流,然后通过Properties类的load(InputStream inStream)方法加载到Properties对象中,最后通过Properties对象来操作文件内容;
5,6是通过ResourceBundle类来加载Properties文件,然后ResourceBundle对象来操做properties文件内容;
其中最重要的就是每种方式加载文件时,文件的路径一定要对。
properties文件以及项目结:
#\u73af\u4fdd\u5927\u68c0\u67e5\u6570\u636e\u5e93\u6570\u636e\u5e93\u76f8\u5173 driverClassName=com.mysql.jdbc.Driver url=jdbc\:mysql\://localhost\:3306/mydatabase?useUnicode\=true&characterEncoding\=utf-8&generateSimpleParameterMetadata\=true username=root password=test123