一 获取 prop属性文件:
1/** 2 * 读取属性文件 3 * Created by lg on 2017/2/3. 4 */ 5public class LoadPropertiesFile { 6 7 public static Map<String,String> loadPropFile(String filePath) { 8 9 Map<String, String> map = new HashMap<String,String>(); 10 11 // 获取资源文件 12 InputStream is = LoadPropertiesFile.class.getClassLoader().getResourceAsStream(filePath); 13 //属性列表 14 Properties prop = new Properties(); 15 16 try { 17 prop.load(is); 18 19 } catch (IOException e) { 20 e.printStackTrace(); 21 } 22 Set<Map.Entry<Object, Object>> set = prop.entrySet(); 23 Iterator<Map.Entry<Object, Object>> iterator = set.iterator(); 24 String key = null; 25 String value = null; 26 while (iterator.hasNext()) { 27 Map.Entry<Object, Object> entry = iterator.next(); 28 key = String.valueOf(entry.getKey()); 29 value = String.valueOf(entry.getValue()); 30 //对取出的值进行非空处理 31 key = key == null? null :key.trim().toUpperCase(); 32 value = value == null? null :value.trim().toUpperCase(); 33 map.put(key, value); 34 } 35 return map; 36 } 37}
二 获取普通文件
URL url = YourClass.class.getClassLoader().getResource("extObj.txt"); File file = new File(url.getFile());
三 获取文件输入流
InputStream in = YourClass.class.getClassLoader().getResourceAsStream(fileName);