它们都是去取classpath下的资源,但它们在路径上有一些区别:
1、Class.getResourceAsStream如果在没有"/"开头的情况下是去相对于此class文件开始进行查询的,如:如果有一个com.elliot.A.class的类文件,如果用A.class.getResourceAsStream("test.xml"),其是去A.class包下即com/elliot/下去查找test.xml文件的。但如果其以A.class.getResourceAsStream("/test.xml"),就是从classpath的绝对路径去查找test.xml文件的,也就是和包com同级下去查找文件。
2、ClassLoader.getResourceAsStream方式是从classpath的绝对路径开始去查询资源的,其开头不能加"/",其效果和Class.getResourceAsStream以"/"等同。
相关推荐
主要介绍了Java 使用getClass().getResourceAsStream()方法获取资源的相关资料,这里主要讲解哪种方式可以获取到文件资源,需要的朋友可以参考下
Java加载资源文件的两种方法getResource与getResourceAsStream
1。使用java.util.Properties类的load()方法 示例: InputStream in = lnew ...InputStream in = JProperties.class.getResourceAsStream(name); Properties p = new Properties(); p.load(in);
InputStream inStream = DaoFactory.class.getClassLoader() .getResourceAsStream("daoconfig.properties"); prop.load(inStream); String userDaoClass = prop.getProperty("userDaoClass"); ...
刚开始简单破解了一下。 生成PDF都没问题,后来发现生成... InputStream is = XXX.class.getResourceAsStream("/pdflicense.xml"); license.setLicense(is); is.close(); 其中pdflicense.xml已经包含在jar文件里面
aspose.slides-16.10.0.jar破解版,亲测可用。...InputStream input = AsposeUtil.class.getResourceAsStream("license.xml"); License license = new com.aspose.slides.License(); license.setLicense(input);
InputStream is = com.aspose.words.Document.class.getResourceAsStream("/com.aspose.words.lic_2999.xml"); License aposeLic = new License(); aposeLic.setLicense(is); result = true; is.close(); } ...
InputStream is = com.aspose.words.Document.class .getResourceAsStream("/com.aspose.words.lic_2999.xml"); License asposeLicense = new License(); asposeLicense.setLicense(is); is.close(); } catch ...
InputStream is = Demo1.class.getResourceAsStream("/students.xml");// src目录下 // 1. 获得文档解析器工厂对象 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); // 2. ...
Java中getResourceAsStream的用法.md
InputStream is = DruidJDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"); prop.load(is); ds = DruidDataSourceFactory.createDataSource(prop); } catch (IOException e) { e....
java项目读取配置文件的实例代码,使用class.getClassLoader().getResourceAsStream方法。
InputStream inputStream = CommonTest.class.getClassLoader().getResourceAsStream("mybatis-configuration.xml"); SQLSessionFactory SQLSessionFactory = new SQLSessionFactoryBuilder().build(inputStream)...
in = MyTask.class.getClassLoader().getResourceAsStream("db.properties"); prop.load(in); this.sql_ip=prop.getProperty("DB_IP").trim(); this.sql_db_name=prop.getProperty("DB_NAME").trim(); this.sql...
is = ReadProperties.class.getClassLoader().getResourceAsStream( strFilePath); if (is == null) { WriteLog.writeLog("閰嶇疆鏂囦欢涓嶅瓨鍦紝璇锋鏌ユ枃浠跺悕鎴栬矾寰勬槸鍚︽纭紒"); ...
List<WeatherInfos> infos = weatherService.getWeatherInfos(MainActivity.class.getClassLoader().getResourceAsStream("z.xml")); StringBuffer sb = new StringBuffer(); for(WeatherInfos info : infos){ ...
prop.load(DButil.class.getResourceAsStream("/db_config.properties")); info = new DButil(); info.driver = prop.getProperty("driver"); info.dbName = prop.getProperty("dbName"); info.host = prop....
InputStream in = getClass().getResourceAsStream("/config.properties"); try { props.load(in); Enumeration en = props.propertyNames(); System.out.print("读取配置文件:"); while(en....
public class ClassXmlPathApplicationContext implements BeanFactory { private Map, Object> map = new HashMap, Object>(); public ClassXmlPathApplicationContext(String fileName) throws Exception, ...
InputStream is = JdbcUtil.class .getClassLoader() .getResourceAsStream(DBINFO_FIIE_NAME); try { infos.load(is); } catch (Exception e) { e.printStackTrace(); throw new ...