java 路径、className.class.getResourceAsStream()、ClassLoader.getSystemResourceAsStream() 、FileInputStream
className.class.getResourceAsStream 用法:
第一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类Test.class ,同时有资源文件config.properties
那么,应该有如下代码:
//前面没有“/”代表当前类的目录
InputStream is1 = Test.class.getResourceAsStream("config.properties");
System.out.println(is1);// 不为null
第二:在Test.class目录的子目录下,例如:com.x.y 下有类Test.class ,同时在 com.x.y.prop目录下有资源文件config.properties
那么,应该有如下代码:
//前面没有“/”代表当前类的目录
InputStream is2 = Test.class.getResourceAsStream("prop/config.properties");
System.out.println(is2);//不为null
第三:不在同目录下,也不在子目录下,例如:com.x.y 下有类Test.class ,同时在 com.m.n 目录下有资源文件config.properties
那么,应该有如下代码:
//前面有“/”,代表了工程的根目录
InputStream is3 = Test.class.getResourceAsStream("/com/m/n/config.properties");
System.out.println(is3);//不为null
ClassLoader.getSystemResourceAsStream 用法:
和className.class.getResourceAsStream 的第三种取得的路径一样,但少了“/”
InputStream is4 = ClassLoader.getSystemResourceAsStream("properties/PayManagment_Config.properties");
System.out.println(is4);//不为null
FileInputStream 用法:
和前面的不一样,
一:例如:com.x.y 下有类Test.class ,用上面的三种不同路径取得的结果都是找不到路径:
代码:
FileInputStream fis;
try {
fis = new FileInputStream("config.properties");
//fis = new FileInputStream("prop/config.properties");
//fis = new FileInputStream("/com/m/n/config.properties");
//fis = new FileInputStream("com/m/n/config.properties");
System.out.println(fis);
} catch (FileNotFoundException e) {
System.out.println("找不到路径");
}
结果都是:找不到路径
为什么呢?
再试试:
一 单独的类:
String projectPath = System.getProperty("user.dir");//main方法取得的是当前项目路径
//System.out.println(projectPath);
fis = new FileInputStream(projectPath + "/src/com/m/n/config.properties");
System.out.println(fis);//不为空了
二:
但是我在做rcp项目时,System.getProperty("user.dir");取得的路径就是D:/Program Files/eclipse, 我的eclipse 安装路径,所以我把资源文件直接放eclipse目录底下
fis = new FileInputStream("config.properties");
System.out.println(fis);//不为空了
不知道为什么?现在只是知道了这几种方法肯定有一种适合,但还是有点模糊不清,望懂得的人指点。
//前面没有“/”代表当前类的目录
java 路径、className.class.getResourceAsStream()、ClassLoader.getSystemResourceAsStream() 、FileInputStream的更多相关文章
- className.class.getResourceAsStream与ClassLoader.getSystemResourceAsStream区别
className.class.getResourceAsStream : 一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类Test.class ,同时有资源文件conf ...
- Java路径问题最终解决方案—可定位所有资源的相对路径寻址
1.在Java项目中,应该通过绝对路径访问文件,以下为访问的常用方法: 第一种方法:类名.class.getResource("/").getPath()+文件名 第二种方法:Th ...
- Java路径问题终于解决方式—可定位全部资源的相对路径寻址
1.在Java项目中,应该通过绝对路径訪问文件.下面为訪问的经常用法: 第一种方法:类名.class.getResource("/").getPath()+文件名称 另外一种方法: ...
- ClassLoader.getSystemResourceAsStream()
一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类Test.class ,同时有资源文件config.properties 那么,应该有如下代码: //前面没有" ...
- ClassLoader.getSystemResourceAsStream("a.txt")获取不到资源文件
一.解决方案 换成XXX.class.getClassLoader().getResourceAsStream("a.txt")即可. 二.场景复现 src/main/resour ...
- 【Java基础】通过getResourceAsStream() 加载资源文件
Class.getResourceAsStream(String path) path不以"/"开头时,默认是从当前类所在的包下面获取资源 path以"/"开头 ...
- When a java class is load by classloader, where the constant poll be put?
Q:When a java class is load by classloader, where the constant poll be put? A:the "Non-Heap Mem ...
- java中class.forName和classLoader加载类的区分
java中class.forName和classLoader都可用来对类进行加载.前者除了将类的.class文件加载到jvm中之外,还会对类进行解释,执行类中的static块.而classLoade ...
- 一篇文章带你吃透,Java界最神秘技术ClassLoader
ClassLoader 是 Java 届最为神秘的技术之一,无数人被它伤透了脑筋,摸不清门道究竟在哪里.网上的文章也是一篇又一篇,经过本人的亲自鉴定,绝大部分内容都是在误导别人.本文我带读者彻底吃透 ...
随机推荐
- mysql插入json数据
data_dict = {"a":1, "b":2} data_json = json.dumps(data_dict) data_escape = MySQ ...
- Redis进阶实践之一VMWare Pro虚拟机安装和Linux系统的安装
一.引言 设计模式写完了,相当于重新学了一遍,每次学习都会有不同的感受,对设计模式的理解又加深了,理解的更加透彻了.还差一篇关于设计模式的总结的文章了,写完这篇总结性的文章,设计模式的文章就暂时要告一 ...
- 【340】GIS related knowledge
mpk: 对地图文档以及所有引用的数据源进行打包可创建经过压缩的单个 .mpk 文件.参考,可以通过 Package Map 工具实现. Collector for ArcGIS Survey123 ...
- 本博客已经迁移去http://blog.brightwang.com/
本博客已经迁移去http://blog.brightwang.com/ ,感谢各位支持.
- 第八章 高级搜索树 (b5)B-树: 删除
- 如何搭建hibernate框架
我写这篇博客,主要是想让大家能够快速上手hibernate,本人建议学习框架,应该一个框架一个框架学习,别一上手就三大框架整合,学习之类的.这里只是单独搭建hibernate框架,让大家 能够更好的上 ...
- 最小重组缓冲区和路径MTU发现
概括: 主要来源于unp,可参考:http://blog.csdn.net/ysu108/article/details/7764461 疑惑: 1. 最小重组缓冲区大小: ipv4为576,ipv6 ...
- TZOJ 1840 Jack Straws(线段相交+并查集)
描述 In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the ta ...
- boost x64 lib
libboost_atomic-vc150-mt-gd-x64-1_66.liblibboost_atomic-vc150-mt-s-x64-1_66.liblibboost_atomic-vc150 ...
- iOS - push 或 pop或点击导航栏返回pop指定导航控制器
以前一直有个很疑惑的问题没有搞清楚 关于ios中 viewcontroller的跳转问题,其中有一种方式是采用navigationController pushViewController 的方法,比 ...