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

1/* 范例名称:java读取properties文件总结 2 * 源文件名称:PropertiesFileReadTest.java 3 * 要 点: 4 * 1. 使用getResourceAsStream方法读取properties文件 5 * 2. 使用InPutStream流读取properties文件 6 * 3. 读取properties文件的路径写法问题 7 * 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 public static void main(String[] args) throws FileNotFoundException { 21 readPropFileByGetResourceAsAtream(); 22 System.out.println("--------------"); 23 //readPropFileByInPutStream(); 24 } 25 26 /* 27 * 使用getResourceAsAtream方法读取 28 */ 29 private static void readPropFileByGetResourceAsAtream() { 30 /* 31 * 读取src下面config.properties包内的配置文件 test1.properties位于config.properties包内 32 */ 33 InputStream inl = PropertiesFileReadTest.class.getClassLoader() 34 .getResourceAsStream("config/properties/test1.properties"); 35 36 /* 37 * 读取和PropertiesFileReadTest类位于同一个包里面的配置文件 38 * test2.properties和PropertiesFileReadTest类在同一个包内 39 */ 40 InputStream in2 = PropertiesFileReadTest.class.getResourceAsStream("test2.properties"); 41 42 /* 43 * 读取src根目录下文件的配置文件 jdbc.properties位于src目录 44 */ 45 InputStream in3 = PropertiesFileReadTest.class.getClassLoader().getResourceAsStream("jdbc.properties"); 46 47 /* 48 * 读取位于另一个source文件夹里面的配置文件 config是一个source文件夹,config.properties位于config 49 * source文件夹中 50 */ 51 InputStream in4 = PropertiesFileReadTest.class.getClassLoader().getResourceAsStream("config.properties"); 52 53 Properties properties = new Properties(); 54 System.out.println("----使用getResourceAsStream方法读取properties文件----"); 55 56 // 从输入字节流读取属性列表(键,值) 57 try { 58 System.out.println("-----------------------"); 59 properties.load(inl); 60 System.out.println("test1.properties:name=" + properties.getProperty("name") + ",age=" 61 + properties.getProperty("age")); 62 System.out.println("-----------------------"); 63 System.out.println("-----------------------"); 64 properties.load(in2); 65 System.out.println("test2.properties:name=" + properties.getProperty("name") + ",age=" 66 + properties.getProperty("age")); 67 System.out.println("-----------------------"); 68 properties.load(in3); 69 System.out.println("jdbc.properties:"); 70 // 使用指定的格式字符串和参数返回格式化的字符串, 这里的%s是java String占位符 71 System.out.println(String.format("jdbc.url=%s", properties.getProperty("jdbc.url"))); 72 System.out.println(String.format("jdbc.usename=%s", properties.getProperty("jdbc.usename"))); 73 System.out.println(String.format("jdbc.password=%s", properties.getProperty("jdbc.password"))); 74 properties.load(in4); 75 System.out.println("config.properties:"); 76 // 使用给定的模式创建一个MessageFormat,并使用它来格式化给定的参数,{0}是一个java的字符串占位符 77 System.out.println(MessageFormat.format("dbuser={0}", properties.getProperty("dbuser"))); 78 System.out.println(MessageFormat.format("dbpassword={0}", properties.getProperty("dbpassword"))); 79 System.out.println(MessageFormat.format("database={0}", properties.getProperty("database"))); 80 System.out.println("----------------------------------------------"); 81 82 } catch (IOException e) { 83 // TODO Auto-generated catch block 84 e.printStackTrace(); 85 }finally { 86 if(inl != null) { 87 try { 88 inl.close(); 89 } catch (IOException e) { 90 e.printStackTrace(); 91 } 92 } 93 if(in2 != null) { 94 try { 95 inl.close(); 96 } catch (IOException e) { 97 e.printStackTrace(); 98 } 99 } 100 if(in3 != null) { 101 try { 102 inl.close(); 103 } catch (IOException e) { 104 e.printStackTrace(); 105 } 106 } 107 if(in4 != null) { 108 try { 109 inl.close(); 110 } catch (IOException e) { 111 e.printStackTrace(); 112 } 113 } 114 115 } 116 } 117 118/* 119 * 使用InputStream流读取properties 120 */ 121 private static void readPropFileByInPutStream() throws FileNotFoundException { 122 InputStream in1=null; 123 InputStream in2=null; 124 InputStream in3=null; 125 InputStream in4=null; 126 System.out.println("----使用InputStream流读取properties文件----"); 127 try { 128 /* 129 * 读取src下面config.properties包内的配置文件 test1.properties位于config.properties包内 130 */ 131 132 in1 =new BufferedInputStream(new FileInputStream("src/config/properties/test1.properties")); 133 /* 134 * 读取和PropertiesFileReadTest类位于同一个包里面的配置文件 135 * test2.properties和PropertiesFileReadTest类在同一个包里面 136 */ 137 in2=new BufferedInputStream(new FileInputStream("src/propertiesFile/read/test/test2.properties")); 138 /* 139 * 读取src根目录下文件的配置文件 140 * jdbc.properties位于src目录 141 */ 142 in3 = new BufferedInputStream(new FileInputStream("src/jdbc.properties")); 143 /* 144 * 读取位于另一个source文件夹里面的配置文件 145 * config是一个source文件夹,config.properties位于config source文件夹中 146 */ 147 in4 = new FileInputStream("config/config.properties"); 148 149 Properties properties=new Properties(); 150 151 System.out.println("-----------------------"); 152 properties.load(in1); 153 System.out.println("test1.properties:name=" + properties.getProperty("name") + ",age=" 154 + properties.getProperty("age")); 155 System.out.println("-----------------------"); 156 System.out.println("-----------------------"); 157 properties.load(in2); 158 System.out.println("test2.properties:name=" + properties.getProperty("name") + ",age=" 159 + properties.getProperty("age")); 160 System.out.println("-----------------------"); 161 properties.load(in3); 162 System.out.println("jdbc.properties:"); 163 // 使用指定的格式字符串和参数返回格式化的字符串, 这里的%s是java String占位符 164 System.out.println(String.format("jdbc.url=%s", properties.getProperty("jdbc.url"))); 165 System.out.println(String.format("jdbc.usename=%s", properties.getProperty("jdbc.usename"))); 166 System.out.println(String.format("jdbc.password=%s", properties.getProperty("jdbc.password"))); 167 properties.load(in4); 168 System.out.println("config.properties:"); 169 // 使用给定的模式创建一个MessageFormat,并使用它来格式化给定的参数,{0}是一个java的字符串占位符 170 System.out.println(MessageFormat.format("dbuser={0}", properties.getProperty("dbuser"))); 171 System.out.println(MessageFormat.format("dbpassword={0}", properties.getProperty("dbpassword"))); 172 System.out.println(MessageFormat.format("database={0}", properties.getProperty("database"))); 173 System.out.println("----------------------------------------------"); 174 175 176 177 178 } catch (IOException e) { 179 // TODO Auto-generated catch block 180 e.printStackTrace(); 181 }finally { 182 if (in1 != null) { 183 try { 184 in1.close(); 185 } catch (IOException e) { 186 e.printStackTrace(); 187 } 188 } 189 190 if (in2 != null) { 191 try { 192 in2.close(); 193 } catch (IOException e) { 194 e.printStackTrace(); 195 } 196 } 197 198 if (in3 != null) { 199 try { 200 in3.close(); 201 } catch (IOException e) { 202 e.printStackTrace(); 203 } 204 } 205 206 if (in4 != null) { 207 try { 208 in4.close(); 209 } catch (IOException e) { 210 e.printStackTrace(); 211 } 212 } 213 } 214 215 }