加载指定文件的方式,使用getResourceAsStream("fileName")
来源:互联网 发布:网络问诊国家政策 编辑:程序博客网 时间:2024/06/10 16:36
在Java Web等工程中,我们可以使用下面的方式来加载一个文件流
InputStream in=DBUtil.class.getResourceAsStream("database.properties")
此时,database.properties 和 DBUtil 在同一级目录下面。
getResourceAsStream方法解释:
Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object’s class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream.
Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
• If the name begins with a ‘/’ (‘\u002f’), then the absolute name of the resource is the portion of the name following the ‘/’.
• Otherwise, the absolute name is of the following form:
modified_package_name/name
Where the modified_package_name is the package name of this object with ‘/’ substituted for ‘.’ (‘\u002e’).
中文解释:
通过一个名称找到资源。搜索规则跟给定的类有关系,它是通过定义这个类的类加载器来实现的。
这个方法会委派给这个对象的类加载器,如果这个对象是由启动类加载器加载的,那么方法就会为派给ClassLoader.getSystemResourceAsStream
在委派之前,一个资源路径是由给定的资源名称通过下面的算法来构造的:
如果资源的名称由/开头,那么资源的绝对名称就是/后面的那一部分,
否则的话,绝对名称就是:modified_package_name/name
modified_package_name是这个对象的包名用’/’替代’.’,因为包的形式是com.abc.ClassA.class这样的形式,实际上是com/abc/ClassA.class
总结:
也就是说,只要资源和类在同一级目录下面,无论资源名称的前面加不加/或./都可以找到。
- 加载指定文件的方式,使用getResourceAsStream("fileName")
- JAVA 与 MyCat(3) 关于getResourceAsStream(fileName)的文件位置
- Class.getResourceAsStream和ClassLoader.getResourceAsStream加载文件路径问题
- Spring加载properties文件指定编码方式
- C语言文件指针的使用...long file_size2(char * filename)
- 加载文件的方式
- getResource于getResourceAsStream的使用
- 加载SpringContext文件的方式
- java加载文件的方式
- 读取filename文件的内容到wstring
- MDK中加载指定文件的技巧
- linux加载指定目录的so文件
- MDK中加载指定文件的技巧
- getResourceAsStream, 修改war包内的文件
- 使用FTP下载文件connect.retrieveFileStream(filename) 获取不到InputStream流,返回null的问题
- java中使用相对路径读取文件的写法总结 以及getResourceAsStream()
- android 指定文件打开的方式
- C# 使用覆盖文件的方式发送byte[]到指定的打印机
- java 获取 hbase数据 springdatahadoop -- hbasetemplate
- laravel DB类SQL语句操作(CURD)
- CSS 盒子模型
- 并发编程--互斥锁ReentrantLock
- stderr和stdout详细解说
- 加载指定文件的方式,使用getResourceAsStream("fileName")
- GUI_对四则运算案例加入校验和提示
- freemarker中使用shiro标签
- tensorflow实战系列(四)基于TensorFlow构建AlexNet代码解析
- 关于Xfermode中src和dsc的问题
- Java 获取前端页面代码段中img标签的src属性值,即获取图片Url
- 关于TabLayout遇到的坑
- Linux的ioctl和fcntl
- BZOJ4811 [Ynoi2017]由乃的OJ