Java里读取资源文件时,经常用到的两个方法,但是经常会用错的方法
方法说明如下
public URL getResource(String name)
Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.
The name of a resource is a '/'-separated path name that identifies the resource.
这句明确说明,name参数是以‘/’来作为路径分隔符,不是File.separator,如果使用了File.separator,正确的路径也返回null
This method will first search the parent class loader for the resource; if the parent is null the path of the class loader built-in to the virtual machine is searched. That failing, this method will invoke findResource(String) to find the resource.
Parameters:
name - The resource name
Returns:
A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.
Since:
方法说明如下
引用
public URL getResource(String name)
Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.
The name of a resource is a '/'-separated path name that identifies the resource.
这句明确说明,name参数是以‘/’来作为路径分隔符,不是File.separator,如果使用了File.separator,正确的路径也返回null
This method will first search the parent class loader for the resource; if the parent is null the path of the class loader built-in to the virtual machine is searched. That failing, this method will invoke findResource(String) to find the resource.
Parameters:
name - The resource name
Returns:
A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.
Since:
发表评论
-
charles4.2下载与破解方法以及配置https
2020-02-26 09:03 2有两个抓包工具 一个是fidder,一个是charles,两个 ... -
序列号批量生成算法
2019-12-05 14:11 0业务处理过程当中,经常需要生成订单号、序列号等,简单的可 ... -
使用ANTLR处理文本
2019-08-28 17:32 725引用 使用 Antlr 处理文本 https://www.ib ... -
解决maven-metadata.xml文件下载卡死问题
2019-04-11 14:02 3750http://192.168.1.110:8081/nexus ... -
rsync备份和删除指定文件
2018-01-02 10:23 1973文件异地备份时,需要将本地文件合并到服务器上,且不能删除服务器 ... -
javaLocale格式化日期和数字
2017-08-25 09:26 814public static void main(Strin ... -
centos6 tomcat 启动脚本 tomcat服务
2017-08-23 11:24 1371系统自动启动tomcat 复制该脚本到/etc/init.d/ ... -
win7 命令行改IP和DNS
2016-12-21 18:35 655使用管理员权限运行CMD //改DNS netsh ... -
jenkins中集成sonar,使用findbug、pmd、checkstyle提升代码质量
2016-09-29 14:58 6075实际上jenkins单独也 ... -
jenkins 集成sonar
2016-09-18 10:14 0jenkins集成sonar可以从插件中心直接更新安装 son ... -
activeMQ5.14权限配置
2016-08-17 13:47 2607activeMQ默认的消息队列没有用户名和密码,可以直接通过T ... -
solaris 使用解压版的jdk
2016-07-27 15:17 708solaris上配置jdk其实也很简单 由于solaris有 ... -
solaris tomcat开机启动
2016-07-27 16:17 563创建文件夹/var/svc/manifes ... -
HibernateTemplate Vs HibernateDaoSupport Vs Direct Hibernate Access
2016-07-26 11:07 676http://forum.spring.io/forum/sp ... -
spring mvc mybatis will not be managed by Spring
2016-07-20 17:30 9632项目运行时发现事务提交不完整,回滚时只能回滚一半。 系统配置 ... -
java里判断一点是否在某个区域
2016-06-03 17:47 1670import java.awt.geom.Path2D ... -
12306的技术升级
2016-04-20 16:17 986升级的核心是余票查询的升级,余票查询使用存储过程,sybase ... -
工作流的123
2016-04-20 12:58 538三分钟了解Activity工作流 工作流一般会给开发人员提供流 ... -
sping mvc 使用@Value注解为controller注入值
2016-04-17 17:39 10779spring mvc 里有两个配置文件, 第一个,为sprin ... -
googleapis.com域名访问慢的解决办法
2016-04-13 12:09 8861、安装火狐 2、安装插件ReplaceGoogleCDN
相关推荐
Java加载资源文件的两种方法getResource与getResourceAsStream
java getResourceAsStream种类,分我所知道的3种情况
主要介绍了Java 使用getClass().getResourceAsStream()方法获取资源的相关资料,这里主要讲解哪种方式可以获取到文件资源,需要的朋友可以参考下
资源文件加载类 这里是用的是class getResourceAsStream "path" 来加载资源文件的
在java类里直接读取配置文件中的信息,用到的方法是this.getClass().getClassLoader().getResourceAsStream
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault()); 3。使用java.util.PropertyResourceBundle类的构造函数 示例: InputStream in = new BufferedInputStream(new FileInputStream...
主要介绍了Java中getResourceAsStream用法,较为详细的分析了getResourceAsStream的功能及用法,需要的朋友可以参考下
java项目读取配置文件的实例代码,使用class.getClassLoader().getResourceAsStream方法。
Java中getResourceAsStream的用法.md
private static final Log log = LogFactory.getLog(ReadProperties.class); /** * 璇诲彇璧勬簮鏂囦欢 * @param strFilePath 鏂囦欢璺緞 * @Author 锛歡aochao */ public static Properties ...
getResourceAsStream( " path/to/resource.txt " ); 此问题影响非Android单元测试和Java资源,仅在具有“单元测试”配置的Android Studio上执行。 它不影响使用Gradle(在Android Studio或命令行中)执行测试用例...
装饰者模式指的是在不必改变原类(Input)文件和使用继承的情况下,动态地扩展一个对象的功能。 它是通过创建一个包装对象,也就是装饰来包裹真实的对象。 实现步骤 :通过对原类文件继承,对原有...
XML用于保存及交换数据,与读取配置文件的类在同一包,或在WEB-INF(或其子目录下),// 读取配置文件获得一个输入流 InputStream is = Demo1.class.getResourceAsStream("/students.xml");// src目录下 // 1. 获得...
<!-- TOC --> - Mybatis源码分析 - 1. 解析配置文件,创建SQLSessionFactory - 2. 开启java程序和数据库之间的会话: ...这一步首先读取了mybatis的configuration xml配置文件,用这个流构造了Factory的Builder,它底
InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);XML 配置文件(configuration XML)中包含了对 ...
11 URL getResource(String path) 返回指定资源(文件及目录)的URL路径 12 InputStream getResourceAsStream(String path) 返回指定资源的输入流 13 RequestDispatcher getRequestDispatcher(String uripath) ...
javaEE-web的文件路径, 用getClass().getResourceAsStream("/")来确定文件路径 并用jdom读取xml和下载1
.getResourceAsStream(DBINFO_FIIE_NAME); try { infos.load(is); } catch (Exception e) { e.printStackTrace(); throw new ExceptionInInitializerError("属性文件加载错误"); } finally { ...
InputStream is = DruidJDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"); prop.load(is); ds = DruidDataSourceFactory.createDataSource(prop); } catch (IOException e) { e....
由Maven管理对于我来说,我更喜欢使用maven和Idea Intellij ,而maven是一个面向项目的管理器,自然地,我更改了项目结构,而不是使用eclipse。 方便地运行命令: mvn clean package ,您将在目标目录中找到该jar 。...