properties文件操作
Java 学习
案例
public static void main(String[] args) { Properties properties = new Properties(); try { properties.load(TestProperties.class.getClassLoader().getResourceAsStream("properties.properties")); } catch (IOException e) { e.printStackTrace(); return; } String a = properties.getProperty("cesar"); System.out.println(a);}
获取properties文件的方式
- 通过绝对路径:InputStream is=new FileInputStream(filePath)
- 通过Class.getResourceAsStream(path)
Class.getResourceAsStream(String path):path不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取。其只是通过path构造一个绝对路径,最终还是由 ClassLoader获取资源。
- 通过ClassLoader.getResourceAsStream(path) Class.getClassLoader.getResourceAsStream(String path) :默认则是从ClassPath根下获取,path不能以’/'开头,最终是由ClassLoader获取资源。