使用classLoader去管理资源文件和配置文件
写代码的时候发现这段关于classLoader的代码读不懂
?
?
String templateFileName = Thread.currentThread().getContextClassLoader().getResource("WeeklyReportDownload_2-template.xls").getFile();?其实就是用类加载器去管理资源文件,顺便就去学习了下
?
Demo:
?
package com.huaxia.utils.learn;import java.io.InputStream;import java.util.Properties;public class ClassLoaderTest {public static void main(String[] args) throws Exception {// 从classpath根目录开始找配置文件InputStream is = ClassLoaderTest.class.getClassLoader().getResourceAsStream("config.properties");Properties prop = new Properties();prop.load(is);is.close();System.out.println(prop.get("apple"));}}?classLoader会从classpath下去查找文件(似乎不会找子文件夹下内容),
资源文件路径如图:
?
大部分框架都是采用这种方式去管理配置文件和资源文件的
?
?
?
?