getResource()和getResourceAsStream以及路径问题
参考文章:http://blog.chinaunix.net/uid-21227800-id-65876.html
Java中的getResourceAsStream用法:
参考文章:https://www.cnblogs.com/macwhirr/p/8116583.html
简单举两个例子:
项目结构:
代码在ResourcesUtil.java里
public static String getValue(String name){ String temp=null; Properties prop = new Properties();// 属性集合对象 FileInputStream fis = null;// 属性文件输入流 try { fis = new FileInputStream(ResourcesUtil.class.getResource("Mould.properties").getFile()); // fis = new FileInputStream("src/main/java/ConfigurationFiles/Mould.properties"); prop.load(fis);// 将属性文件流装载到Properties对象中 fis.close();// 关闭流 temp=prop.getProperty(name); } catch (IOException e) { e.printStackTrace(); } return temp; }
public static String getValue(String name) { String temp=null; Properties pps = new Properties(); InputStream stream = ResourcesUtil.class // .getClassLoader() .getResourceAsStream("Mould.properties"); try { BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8")); pps.load(br); temp=pps.getProperty(name); }catch (IOException e){ e.printStackTrace(); } return temp; }