一、java读取properties文件总结
在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResourceAsStream方法和InputStream流去读取properties文件,使用getResourceAsStream方法去读取properties文件时需要特别注意properties文件路径的写法,测试项目如下:
1.1.项目的目录结构
1.2. java读取properties文件代码测试

1/* 范例名称:java读取properties文件总结 2 * 源文件名称:PropertiesFileReadTest.java 3 * 要 点: 4 * 1. 使用getResourceAsStream方法读取properties文件 5 * 2. 使用InPutStream流读取properties文件 6 * 3. 读取properties文件的路径写法问题 7 * 时间:2014/4/2 8 */ 9package propertiesFile.read.test; 10 11import java.io.BufferedInputStream; 12import java.io.FileInputStream; 13import java.io.FileNotFoundException; 14import java.io.IOException; 15import java.io.InputStream; 16import java.text.MessageFormat; 17import java.util.Properties; 18 19public class PropertiesFileReadTest { 20 21 /** 22 * @param args 23 */ 24 public static void main(String[] args) { 25 try { 26 readPropFileByGetResourceAsStream(); 27 System.out.println(""); 28 readPropFileByInPutStream(); 29 } catch (Exception e) { 30 e.printStackTrace();// TODO: handle exception 31 } 32 } 33 34 /** 35 * 使用getResourceAsStream方法读取properties文件 36 */ 37 static void readPropFileByGetResourceAsStream() { 38 /** 39 * 读取src下面config.properties包内的配置文件 40 * test1.properties位于config.properties包内 41 */ 42 InputStream in1 = PropertiesFileReadTest.class.getClassLoader() 43 .getResourceAsStream("config/properties/test1.properties"); 44 /** 45 * 读取和PropertiesFileReadTest类位于同一个包里面的配置文件 46 * test2.properties和PropertiesFileReadTest类在同一个包里面 47 */ 48 InputStream in2 = PropertiesFileReadTest.class 49 .getResourceAsStream("test2.properties"); 50 /** 51 * 读取src根目录下文件的配置文件 52 * jdbc.properties位于src目录 53 */ 54 InputStream in3 = PropertiesFileReadTest.class.getClassLoader() 55 .getResourceAsStream("jdbc.properties"); 56 /** 57 * 读取位于另一个source文件夹里面的配置文件 58 * config是一个source文件夹,config.properties位于config source文件夹中 59 */ 60 InputStream in4 = PropertiesFileReadTest.class.getClassLoader() 61 .getResourceAsStream("config.properties"); 62 63 Properties p = new Properties(); 64 System.out.println("----使用getResourceAsStream方法读取properties文件----"); 65 try { 66 System.out 67 .println("----------------------------------------------"); 68 p.load(in1); 69 System.out.println("test1.properties:name=" + p.getProperty("name") 70 + ",age=" + p.getProperty("age")); 71 System.out 72 .println("----------------------------------------------"); 73 74 p.load(in2); 75 System.out.println("test2.properties:name=" + p.getProperty("name") 76 + ",age=" + p.getProperty("age")); 77 System.out 78 .println("----------------------------------------------"); 79 80 p.load(in3); 81 System.out.println("jdbc.properties:"); 82 System.out.println(String.format("jdbc.driver=%s", 83 p.getProperty("jdbc.driver")));// 这里的%s是java String占位符 84 System.out.println(String.format("jdbc.url=%s", 85 p.getProperty("jdbc.url"))); 86 System.out.println(String.format("jdbc.usename=%s", 87 p.getProperty("jdbc.usename"))); 88 System.out.println(String.format("jdbc.password=%s", 89 p.getProperty("jdbc.password"))); 90 System.out 91 .println("----------------------------------------------"); 92 93 p.load(in4); 94 System.out.println("config.properties:"); 95 System.out.println(MessageFormat.format("dbuser={0}", 96 p.getProperty("dbuser")));// {0}是一个java的字符串占位符 97 System.out.println(MessageFormat.format("dbpassword={0}", 98 p.getProperty("dbpassword"))); 99 System.out.println(MessageFormat.format("database={0}", 100 p.getProperty("database"))); 101 System.out 102 .println("----------------------------------------------"); 103 } catch (IOException e) { 104 // TODO Auto-generated catch block 105 e.printStackTrace(); 106 } finally { 107 if (in1 != null) { 108 try { 109 in1.close(); 110 } catch (IOException e) { 111 e.printStackTrace(); 112 } 113 } 114 115 if (in2 != null) { 116 try { 117 in2.close(); 118 } catch (IOException e) { 119 e.printStackTrace(); 120 } 121 } 122 123 if (in3 != null) { 124 try { 125 in3.close(); 126 } catch (IOException e) { 127 e.printStackTrace(); 128 } 129 } 130 131 if (in4 != null) { 132 try { 133 in4.close(); 134 } catch (IOException e) { 135 e.printStackTrace(); 136 } 137 } 138 } 139 } 140 141 /** 142 * 使用InPutStream流读取properties文件 143 */ 144 static void readPropFileByInPutStream() { 145 InputStream in1 = null; 146 InputStream in2 = null; 147 InputStream in3 = null; 148 InputStream in4 = null; 149 System.out.println("----使用InputStream流读取properties文件----"); 150 try { 151 /** 152 * 读取src根目录下文件的配置文件 153 * jdbc.properties位于src目录 154 */ 155 in1 = new BufferedInputStream(new FileInputStream( 156 "src/jdbc.properties")); 157 /** 158 * 读取src下面config.properties包内的配置文件 159 * test1.properties位于config.properties包内 160 */ 161 in2 = new BufferedInputStream(new FileInputStream( 162 "src/config/properties/test1.properties")); 163 /** 164 * 读取和PropertiesFileReadTest类位于同一个包里面的配置文件 165 * test2.properties和PropertiesFileReadTest类在同一个包里面 166 */ 167 in3 = new BufferedInputStream(new FileInputStream( 168 "src/propertiesFile/read/test/test2.properties")); 169 /** 170 * 读取位于另一个source文件夹里面的配置文件 171 * config是一个source文件夹,config.properties位于config source文件夹中 172 */ 173 in4 = new FileInputStream("config/config.properties"); 174 175 Properties p = new Properties(); 176 System.out 177 .println("----------------------------------------------"); 178 179 p.load(in1); 180 System.out.println("jdbc.properties:"); 181 System.out.println(String.format("jdbc.driver=%s", 182 p.getProperty("jdbc.driver")));// 这里的%s是java String占位符 183 System.out.println(String.format("jdbc.url=%s", 184 p.getProperty("jdbc.url"))); 185 System.out.println(String.format("jdbc.usename=%s", 186 p.getProperty("jdbc.usename"))); 187 System.out.println(String.format("jdbc.password=%s", 188 p.getProperty("jdbc.password"))); 189 System.out 190 .println("----------------------------------------------"); 191 192 p.load(in2); 193 System.out.println("test1.properties:name=" + p.getProperty("name") 194 + ",age=" + p.getProperty("age")); 195 System.out 196 .println("----------------------------------------------"); 197 p.load(in3); 198 System.out.println("test2.properties:name=" + p.getProperty("name") 199 + ",age=" + p.getProperty("age")); 200 System.out 201 .println("----------------------------------------------"); 202 203 p.load(in4); 204 System.out.println("config.properties:"); 205 System.out.println(MessageFormat.format("dbuser={0}", 206 p.getProperty("dbuser")));// {0}是一个java的字符串占位符 207 System.out.println(MessageFormat.format("dbpassword={0}", 208 p.getProperty("dbpassword"))); 209 System.out.println(MessageFormat.format("database={0}", 210 p.getProperty("database"))); 211 System.out 212 .println("----------------------------------------------"); 213 } catch (FileNotFoundException e) { 214 e.printStackTrace(); 215 } catch (IOException e) { 216 e.printStackTrace(); 217 } finally { 218 if (in1 != null) { 219 try { 220 in1.close(); 221 } catch (IOException e) { 222 e.printStackTrace(); 223 } 224 } 225 226 if (in2 != null) { 227 try { 228 in2.close(); 229 } catch (IOException e) { 230 e.printStackTrace(); 231 } 232 } 233 234 if (in3 != null) { 235 try { 236 in3.close(); 237 } catch (IOException e) { 238 e.printStackTrace(); 239 } 240 } 241 242 if (in4 != null) { 243 try { 244 in4.close(); 245 } catch (IOException e) { 246 e.printStackTrace(); 247 } 248 } 249 } 250 } 251 252}

运行结果: