h3 { margin-top: 0.46cm; margin-bottom: 0.46cm; line-height: 173%; page-break-inside: avoid; }h3.western { font-family: "DejaVu Serif",serif; font-size: 16pt; }h3.cjk { font-family: "DejaVu Sans"; font-size: 16pt; }h3.ctl { font-family: "DejaVu Sans"; font-size: 16pt; }p { margin-bottom: 0.21cm; }
用类加载器加载配置文件
InputStream inp = ReflectTest2. class .getClassLoader().getResourceAsStream( "cn/itcast/day1/config.properties" );
注意 此写法目录的根目录不需要用‘/’ 表示如果用 ‘/’ 表示 反倒要出错
用class 的getResourceAsStream 方法得到资源文件
此方法在内部也是调用的类加载器
此方法写资源文件名字为资源文件的相对路径 默认和类的路径相同
相对路径读资源文件方法
InputStream inp =ReflectTest2. class .getResourceAsStream( "config.properties" );
如果资源文件的包在类的子包下面 如 resources 包 写法为
InputStream inp =ReflectTest2. class .getResourceAsStream( "resources/config.properties" );
绝对路径读资源文件的方法
InputStream inp =ReflectTest2. class .getResourceAsStream( "/cn.itcast/day1/config.properties" );
注意 根目录需要用 ‘/’