InputStream in = getClass().getResourceAsStream('/'+"spring-beans.dtd"); 表示从classs目录下面的找文件,文件放在src下面就可以了.
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);

 
 
 
(2) 例子
 
package ccutse.DB;
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);获取配置文件的方法的更多相关文章

  1. Class.getResourceAsStream和ClassLoader.getResourceAsStream方法

    项目中,有时候要读取当前classpath下的一些配置文件,下面介绍下Class.getResourceAsStream和ClassLoader.getResourceAsStream两种方法以及两者 ...

  2. 对Class.getResourceAsStream和ClassLoader.getResourceAsStream方法所使用的资源路径的解释

    这是个非常基础的问题了,这里提供一些演示样例,帮助高速理解和记忆这个问题. 在该方法的文档:http://docs.oracle.com/javase/7/docs/api/java/lang/Cla ...

  3. Java代码中获取配置文件(config.properties)中内容的两种方法

    方法千千万,本人暂时只总结了两种方法. (1)config.properties中的内容如图 在applicationContext.xml中配置 <!-- 引入配置文件 --> < ...

  4. Django 使用getattr() 方法获取配置文件的变量值

    在django项目的开发过程中,有时需要获取配置文件里的变量值,可以通过下面这样的方式去进行获取 from django.conf import settings item = getattr(set ...

  5. Class.getResourceAsStream()与ClassLoader.getResourceAsStream()获取资源时的路径说明

    Class.getResourceAsStream(): com.xusweeter.iot.ws.vodafone.config.VodafoneServiceConfig.class.getRes ...

  6. spark读取外部配置文件的方法

    spark读取外部配置文件的方法 spark-submit  --files /tmp/fileName /tmp/test.jar 使用spark提交时使用--files参数,spark会将将本地的 ...

  7. 1、ClassLoader.getResourceAsStream() 与Class.getResourceAsStream()的区别

    1.ClassLoader.getResourceAsStream() 与Class.getResourceAsStream()的区别 例如你写了一个MyTest类在包com.test.mycode ...

  8. Java获取配置文件中的属性

    获取配置文件的属性值 example 目标配置文件jdbc.properties,现想要在java类里面调用opcl的url jdbc.url=jdbc:mysql://localhost:3306/ ...

  9. web中纯java获取配置文件中的数据

    /*********获取配置文件,但配置文件中的值改变,不会随着值的改变也获取的参数值改变**********/  /**   * 原因是因为,类装载,装载完后,不会再去装载了   * *///  I ...

随机推荐

  1. HttpWebRequest 知识点

    string Url = System.Configuration.ConfigurationManager.AppSettings["CallPaperInvoiceURL"]; ...

  2. 使用Ajax验证用户名

    Ajax是一项很重要的技术,下面简要举个例子,来解释如何使用Ajax.步骤如下:使用Ajax验证用户名使用文本框的onBlur事件 使用Ajax技术实现异步交互创建XMLHttpRequest对象通过 ...

  3. Visual Basic for Application

    Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'The note of Visual Basic for Applicati ...

  4. PyCharm 恢复默认设置 | JetBrains IDE 配置文件安装目录

    网上的答案为什么都乱七八糟并且全都全篇一律?某度知道是发源地? 先说 Mac 按需运行下面的 rm 删除命令 # Configuration rm -rf ~/Library/Preferences/ ...

  5. 浅析Python3中的bytes和str类型 (转)

    原文出处:https://www.cnblogs.com/chownjy/p/6625299.html#undefined Python 3最重要的新特性之一是对字符串和二进制数据流做了明确的区分.文 ...

  6. java操作Excel的poi 遍历一个工作簿

    遍历一个工作簿 package com.java.poi; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.h ...

  7. 【转载】intellij idea如何将web项目打成war包

    1.点击[File]->[Project Structure]菜单(或使用Shift+Ctrl+Alt+S快捷键),打开[Project Structure]窗口.如下图: 2.在[Projec ...

  8. 有关微信小程序

    每个页面都要在app.json中配置 "pages": [ "pages/index/index", "pages/list/list", ...

  9. 【codeforces 793C】Mice problem

    [题目链接]:http://codeforces.com/contest/793/problem/C [题意] 给你每个点x轴移动速度,y轴移动速度; 问你有没有某个时刻,所有的点都"严格& ...

  10. Vue.js教程—1.介绍和安装

    Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的渐进式框架.Vue 只关注视图层, 采用自底向上增量开发的设计.Vue 的目标是通过尽可能简单的 API 实现响应的数据绑定 ...