正确使用Class.getResourceAsStream("")与Class.getClassLoader().getResourceAsStream("")

简介: 方法getResourceAsStream("")与getResource("")均常被用于获取编译路径下指定的配置文件,用法相似,下面以getResource("")为例展示其正确用法: /** * 无package */ System.out.println(this.getClass().getResource("template.xml").getPath()

方法getResourceAsStream("")与getResource("")均常被用于获取编译路径下指定的配置文件,用法相似,下面以getResource("")为例展示其正确用法:

	/**
	 * 无package
	 */
	System.out.println(this.getClass().getResource("template.xml").getPath());//相对当前Class路径下的template.xml
	System.out.println(this.getClass().getResource("/template.xml").getPath());//classpath根路径下的template.xml
	System.out.println(this.getClass().getClassLoader().getResource("template.xml").getPath());//classpath根路径下的template.xml
	/**
	 * package
	 */
	System.out.println(this.getClass().getResource("package/template.xml").getPath());//相对当前Class路径/package路径下的template.xml
	System.out.println(this.getClass().getResource("/package/template.xml").getPath());//classpath根路径/package路径下的template.xml
	System.out.println(this.getClass().getClassLoader().getResource("package/template.xml").getPath());//classpath根路径/package路径下的template.xml
		

错误用法(注意斜杠):

	/**
	 * 无package
	 */
	System.out.println(this.getClass().getClassLoader().getResource("/template.xml").getPath());//classpath根路径下的template.xml
	/**
	 * package
	 */
	System.out.println(this.getClass().getClassLoader().getResource("/package/template.xml").getPath());//classpath根路径/package路径下的template.xml
		

注意:在Jar包中获取配置文件时,建议通过getResourceAsStream("")的方式编码。

JAVA基础普及:

Classloader 类加载器,用来加载 Java 类到 Java 虚拟机中。与普通程序不同的是。Java程序(class文件)并不是本地的可执行程序。当运行Java程序时,首先运行JVM(Java虚拟机),然后再把Java class加载到JVM里头运行,负责加载Java class的这部分就叫做Class Loader。

JVM本身包含了一个ClassLoader称为Bootstrap ClassLoader,和JVM一样,BootstrapClassLoader是用本地代码实现的,它负责加载核心JavaClass(即所有java.*开头的类)。另外JVM还会提供两个ClassLoader,它们都是用Java语言编写的,由BootstrapClassLoader加载;其中Extension ClassLoader负责加载扩展的Javaclass(例如所有javax.*开头的类和存放在JRE的ext目录下的类),ApplicationClassLoader负责加载应用程序自身的类。

当运行一个程序的时候,JVM启动,运行bootstrapclassloader,该ClassLoader加载java核心API(ExtClassLoader和AppClassLoader也在此时被加载),然后调用ExtClassLoader加载扩展API,最后AppClassLoader加载CLASSPATH目录下定义的Class,这就是一个程序最基本的加载流程。

Best Wishes For You!

目录
相关文章
|
8月前
|
JavaScript 编译器
模块 ""element-plus"" 没有导出的成员 "ElMessage"。你是想改用 "import ElMessage from "element-plus"" 吗?
模块 ""element-plus"" 没有导出的成员 "ElMessage"。你是想改用 "import ElMessage from "element-plus"" 吗?
225 0
关于$(":button") 选择器的理解
关于$(":button") 选择器的理解
276 0
|
自然语言处理
合同结构化文书解析失败,请联系管理员排查:{"code":3001,"message":"File transform error","success":false,"tracerId":"requestId"}报错处理
在使用自然语言处理自学习平台时,标注任务需要上传标注数据,但是使用doc格式上传文件后开始标注时出现了此提示,此篇文章简单介绍下此问题的处理方式。
609 0
合同结构化文书解析失败,请联系管理员排查:{"code":3001,"message":"File transform error","success":false,"tracerId":"requestId"}报错处理
|
JavaScript 前端开发 Java
var oButtonView1 = oView.byId("ButtonView1");
var oButtonView1 = oView.byId("ButtonView1");
var oButtonView1 = oView.byId("ButtonView1");
ng-repeat part2 - How <li ng-repeat="nameF in Ionames">{{nameF}}</li> is parsed
ng-repeat part2 - How <li ng-repeat="nameF in Ionames">{{nameF}}</li> is parsed
ng-repeat part2 - How <li ng-repeat="nameF in Ionames">{{nameF}}</li> is parsed
OPA 13 - ok(oButton,"find the Create button");
Created by Wang, Jerry, last modified on Nov 08, 2015
100 0
OPA 13 - ok(oButton,"find the Create button");
|
缓存
Integer.parseInt("") Integer.valueOf("")和new Integer("")之间的区别
把一个String转换成int有Integer.parseInt("")、 Integer.valueOf("")和new Integer("")这么几种方式,它们之间有什么区别呢?我们可以分别看一下它们的源码 //Integer.parseInt("") public static int parseInt(String s) throws NumberFormatExc
1523 0