获取文件的绝对路径
必须在类路径src
下(目录中不能有中文和空格)
统一的方式,提高代码的可移植性(Linux和Windows都可)
// getResource("")已经定位到 src 目录,只需要填入该文件的相对于src的目录
String path = Thread.currentThread().getContextClassLoader().getResource("文件相对路径").getPath();
资源绑定器
ResourceBundle
获取properties文件
只能绑定XXX.properties
文件,并且文件只能在类路径下,在写路径的时候不能写拓展名
// 在src目录下的classinfo.properties文件,直接以流的方式返回
InputStream in = ResourceBundle bundle = ResourceBundle.getBundle("classinfo")