ClassLoader.getResourceAsStream(name);获取配置文件的方法
InputStream in = getClass().getResourceAsStream("spring-beans.dtd");
表示从当前classs下面的路径找文件
如果是在com.a.b.c.d.Test这个class就表示spring-beans.dtd要放在目录src\com\a\b\c\d下.
SAXBuilder builder = new SAXBuilder();
EntityResolver resolver = new EntityResolver() {
public InputSource resolveEntity(String publicId,
String systemId) {
if (publicId.equals("-//SPRING//DTD BEAN//EN")) {
InputStream in = getClass().getResourceAsStream(
'/'+"spring-beans.dtd");
return new InputSource(in);
} else {
return null;
}
}
};
builder.setEntityResolver(resolver);
import java.util.*;
import java.net.*;
import java.io.*;
public class ReadProp {
private String user,psw,url,driver;
private int maxCon,minCon;
public ReadProp() {
}
public static void main(String[] args) {
ReadProp readprop = new ReadProp();
InputStream in=null;
try{
in=readprop.getClass().getResourceAsStream(
"c:\\db.properties ");
路径应放在classpath下
if(in==null)
System.out.println( "in is null ");
}
Properties prop=new Properties();
try{
prop.load(in);
}
catch(Exception e){
System.out.println( "装载失败 ");
}
}
}
class.getResourceAsStream
用法
首先,Java中的getResourceAsStream有以下几种
Class.getResourceAsStream(String path) : path
不以’/‘开头时默认是从此类所在的包下取资源,以’/‘开头则是从
ClassPath根下获取。其只是通过path构造一个绝对路径,最终还是由ClassLoader获取资源。
2. Class.getClassLoader.getResourceAsStream(String path)
:默认则是从ClassPath根下获取,path不能以’/‘开头,最终是由
ClassLoader获取资源。
3. ServletContext. getResourceAsStream(String
path):默认从WebAPP根目录下取资源,Tomcat下path是否以’/‘开头无所谓,
当然这和具体的容器实现有关。
4. Jsp下的application内置对象就是上面的ServletContext的一种实现。
其次,getResourceAsStream 用法大致有以下几种
第一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类me.class
,同时有资源文件myfile.xml
那么,应该有如下代码
me.class.getResourceAsStream("myfile.xml");
第二:在me.class目录的子目录下,例如:com.x.y 下有类me.class ,同时在 com.x.y.file
目录下有资源文件myfile.xml
那么,应该有如下代码
me.class.getResourceAsStream("file/myfile.xml");
第三:不在me.class目录下,也不在子目录下,例如:com.x.y 下有类me.class ,同时在 com.x.file
目录下有资源文件myfile.xml
那么,应该有如下代码
me.class.getResourceAsStream("/com/x/file/myfile.xml");
总结一下,可能只是两种写法
第一:前面有 “ / ”
“ / ”代表了工程的根目录,例如工程名叫做myproject,“ / ”代表了myproject
me.class.getResourceAsStream("/com/x/file/myfile.xml");
第二:前面没有 “ / ”
代表当前类的目录
me.class.getResourceAsStream("myfile.xml");
me.class.getResourceAsStream("file/myfile.xml");
ClassLoader.getResourceAsStream(name);获取配置文件的方法的更多相关文章
- Class.getResourceAsStream和ClassLoader.getResourceAsStream方法
项目中,有时候要读取当前classpath下的一些配置文件,下面介绍下Class.getResourceAsStream和ClassLoader.getResourceAsStream两种方法以及两者 ...
- 对Class.getResourceAsStream和ClassLoader.getResourceAsStream方法所使用的资源路径的解释
这是个非常基础的问题了,这里提供一些演示样例,帮助高速理解和记忆这个问题. 在该方法的文档:http://docs.oracle.com/javase/7/docs/api/java/lang/Cla ...
- Java代码中获取配置文件(config.properties)中内容的两种方法
方法千千万,本人暂时只总结了两种方法. (1)config.properties中的内容如图 在applicationContext.xml中配置 <!-- 引入配置文件 --> < ...
- Django 使用getattr() 方法获取配置文件的变量值
在django项目的开发过程中,有时需要获取配置文件里的变量值,可以通过下面这样的方式去进行获取 from django.conf import settings item = getattr(set ...
- Class.getResourceAsStream()与ClassLoader.getResourceAsStream()获取资源时的路径说明
Class.getResourceAsStream(): com.xusweeter.iot.ws.vodafone.config.VodafoneServiceConfig.class.getRes ...
- spark读取外部配置文件的方法
spark读取外部配置文件的方法 spark-submit --files /tmp/fileName /tmp/test.jar 使用spark提交时使用--files参数,spark会将将本地的 ...
- 1、ClassLoader.getResourceAsStream() 与Class.getResourceAsStream()的区别
1.ClassLoader.getResourceAsStream() 与Class.getResourceAsStream()的区别 例如你写了一个MyTest类在包com.test.mycode ...
- Java获取配置文件中的属性
获取配置文件的属性值 example 目标配置文件jdbc.properties,现想要在java类里面调用opcl的url jdbc.url=jdbc:mysql://localhost:3306/ ...
- web中纯java获取配置文件中的数据
/*********获取配置文件,但配置文件中的值改变,不会随着值的改变也获取的参数值改变**********/ /** * 原因是因为,类装载,装载完后,不会再去装载了 * */// I ...
随机推荐
- VHDL之User-defined data types
VHDL allows the user to define own data types. 1 user-defined integer types -- This is indeed the pr ...
- QtUI设计:设置控件透明
QT设置按钮控件透明: 代码: //设置按钮 背景 前景 this->ui->ShowCvRGB->setStyleSheet(QString("color:rgba(25 ...
- DB120连接TTL--OpenWRT
DB120 TTL线连接 1.解压文件安装USB TTL PL2303HX 驱动 2.插上usb转ttl设备 3.串口调试 4.连接ttl线到db120 5.The END
- MVC 运行视图出错
IIS服务器web核心没有安装造成的
- java这个404你能解决吗?
前言 本文首发于公众号[我的小碗汤]本公众号免费提供csdn下载服务,海量IT学习资源,如果你准备入IT坑,励志成为优秀的程序猿,那么这些资源很适合你,包括但不限于java.go.python.spr ...
- Tomcat 服务器中jsp页面乱码
<Connector port="80" protocol="HTTP/1.1" connectionTimeout=&quo ...
- maven将jar包添加到本地仓库
mvn install:install-file -Dfile=jar包本地路径 -DgroupId=groupID -DartifactId=ifactId -Dversion=版本 -Dpacka ...
- PAT_A1147#Heaps
Source: PAT A1147 Heaps (30 分) Description: In computer science, a heap is a specialized tree-based ...
- 环境变量、system(day10)
一.环境变量 bash下的环境变量. 每个进程都默认从父进程继承环境变量 bash本身就是一个程序,这个程序运行的时候,bash进程 可以定义只能之自己这个进程中使用的变量,这种变量称为自定义变量. ...
- open-ldap服务安装(1)
LDAP简介 LDAP 代表 轻量级目录访问协议.在我的理解中ldap就是一个数据库. 在LDAP中,目录条目以分层树状结构排序. 传统上,这种结构反映了地理和组织边界,表示国家/地区的条目显示在树的 ...