java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载;
另一种是通过import java.util.ResourceBundle类的getBundle(String baseName)方法加载。
注意:一定要区分路径格式
实现代码如下:
1 package com.util; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.util.Properties; 8 import java.util.PropertyResourceBundle; 9 import java.util.ResourceBundle; 10 11 public class PropertiesUtil { 12 private static String basePath = "src/prop.properties"; 13 private static String name = ""; 14 private static String nickname = ""; 15 private static String password = ""; 16 17 /** 18 * 一、 使用java.util.Properties类的load(InputStream in)方法加载properties文件 19 * 20 */ 21 public static String getName1() { 22 try { 23 Properties prop = new Properties(); 24 InputStream is = new FileInputStream(basePath); 25 prop.load(is); 26 name = prop.getProperty("username"); 27 } catch (FileNotFoundException e) { 28 e.printStackTrace(); 29 } catch (IOException e) { 30 e.printStackTrace(); 31 } 32 return name; 33 } 34 35 /** 36 * 二、 使用class变量的getResourceAsStream()方法 37 * 注意:getResourceAsStream()读取路径是与本类的同一包下 38 * 39 */ 40 public static String getName2() { 41 Properties prop = new Properties(); 42 InputStream is = PropertiesUtil.class 43 .getResourceAsStream("/com/util/prop.properties"); 44 try { 45 prop.load(is); 46 name = prop.getProperty("username"); 47 } catch (IOException e) { 48 e.printStackTrace(); 49 } 50 return name; 51 } 52 53 /** 54 * 三、 55 * 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法 56 * getResourceAsStream(name)方法的参数必须是包路径+文件名+.后缀 否则会报空指针异常 57 * 58 */ 59 public static String getName3() { 60 Properties prop = new Properties(); 61 InputStream is = PropertiesUtil.class.getClassLoader() 62 .getResourceAsStream("com/util/prop.properties"); 63 try { 64 prop.load(is); 65 66 } catch (IOException e) { 67 e.printStackTrace(); 68 } 69 return name; 70 } 71 72 /** 73 * 四、 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法 74 * getSystemResourceAsStream()方法的参数格式也是有固定要求的 75 * 76 */ 77 public static String getName4() { 78 Properties prop = new Properties(); 79 InputStream is = ClassLoader 80 .getSystemResourceAsStream("com/util/prop.properties"); 81 try { 82 prop.load(is); 83 name = prop.getProperty("username"); 84 } catch (IOException e) { 85 e.printStackTrace(); 86 } 87 return name; 88 } 89 90 /** 91 * 五、 使用java.util.ResourceBundle类的getBundle()方法 92 * 注意:这个getBundle()方法的参数只能写成包路径+properties文件名,否则将抛异常 93 * 94 */ 95 public static String getName5() { 96 ResourceBundle rb = ResourceBundle.getBundle("com/util/prop"); 97 password = rb.getString("password"); 98 return password; 99 } 100 101 /** 102 * 六、 使用java.util.PropertyResourceBundle类的构造函数 103 * 104 */ 105 public static String getName6() { 106 try { 107 InputStream is = new FileInputStream(basePath); 108 ResourceBundle rb = new PropertyResourceBundle(is); 109 nickname = rb.getString("nickname"); 110 } catch (FileNotFoundException e) { 111 e.printStackTrace(); 112 } catch (IOException e) { 113 e.printStackTrace(); 114 } 115 116 return nickname; 117 } 118 119 /** 120 * 测试 121 * 122 */ 123 public static void main(String[] args) { 124 System.out.println("name1:" + PropertiesUtil.getName1()); 125 System.out.println("name2:" + PropertiesUtil.getName2()); 126 System.out.println("name3:" + PropertiesUtil.getName3()); 127 System.out.println("name4:" + PropertiesUtil.getName4()); 128 System.out.println("password:" + PropertiesUtil.getName5()); 129 System.out.println("nickname:" + PropertiesUtil.getName6()); 130 } 131 }
文件路径:
prop.properties文件:
1 username=mamama 2 nickname=xiaoma 3 password=123456
输出结果: