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的更多相关文章

  1. className.class.getResourceAsStream与ClassLoader.getSystemResourceAsStream区别

    className.class.getResourceAsStream : 一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类Test.class ,同时有资源文件conf ...

  2. Java路径问题最终解决方案—可定位所有资源的相对路径寻址

    1.在Java项目中,应该通过绝对路径访问文件,以下为访问的常用方法: 第一种方法:类名.class.getResource("/").getPath()+文件名 第二种方法:Th ...

  3. Java路径问题终于解决方式—可定位全部资源的相对路径寻址

    1.在Java项目中,应该通过绝对路径訪问文件.下面为訪问的经常用法: 第一种方法:类名.class.getResource("/").getPath()+文件名称 另外一种方法: ...

  4. ClassLoader.getSystemResourceAsStream()

    一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类Test.class ,同时有资源文件config.properties 那么,应该有如下代码: //前面没有" ...

  5. ClassLoader.getSystemResourceAsStream("a.txt")获取不到资源文件

    一.解决方案 换成XXX.class.getClassLoader().getResourceAsStream("a.txt")即可. 二.场景复现 src/main/resour ...

  6. 【Java基础】通过getResourceAsStream() 加载资源文件

    Class.getResourceAsStream(String path) path不以"/"开头时,默认是从当前类所在的包下面获取资源 path以"/"开头 ...

  7. 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 ...

  8. java中class.forName和classLoader加载类的区分

     java中class.forName和classLoader都可用来对类进行加载.前者除了将类的.class文件加载到jvm中之外,还会对类进行解释,执行类中的static块.而classLoade ...

  9. 一篇文章带你吃透,Java界最神秘技术ClassLoader

    ClassLoader 是 Java 届最为神秘的技术之一,无数人被它伤透了脑筋,摸不清门道究竟在哪里.网上的文章也是一篇又一篇,经过本人的亲自鉴定,绝大部分内容都是在误导别人.本文我带读者彻底吃透 ...

随机推荐

  1. centos多版本python安装pip

    http://www.cnblogs.com/longxiang92/p/5829373.html yum install python-pip 报错 no package python-pip av ...

  2. 关于gevent的一些理解(二)

    3 实际应用 1 zeromq和gevent: zeromq的介绍请参看:http://www.infoq.com/cn/news/2010/09/introduction-zero-mq 假设你已经 ...

  3. nginx 无法访问root权限的文件内容

    问题: 按照的nginx,nginx配置的user  是 nginx,nginx 是root用户启动的.  文件夹A放的那啥是root用户上传的文件. 可 nginx 无法访问 到  文件. 方法: ...

  4. unity 加载读取外部XML

    cfg.xml <rootNode> <category name="网站"> <item name="mainPage"> ...

  5. sql server判断是否为null

    sql server 替换null:isnull(arg,value) 如:select isnull(price,0.0) from orders ,如果price为null的话,用0.0替换 与n ...

  6. Analyzing Microarray Data with R

    1) 熟悉CEL file 从 NCBI GEO (http://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE24460)下载GSE24460. 将得到 ...

  7. HTML的属性和css基础

    1.name属性: name属性,用于指定标签元素的名称,<a>标签内必须提供href或name属性:<a name ="value"> 2.id属性: 1 ...

  8. TensorFlow—多层感知器—MNIST手写数字识别

    1 import tensorflow as tf import tensorflow.examples.tutorials.mnist.input_data as input_data import ...

  9. KO ------- 表中字段名和实体类属性名不一致

    -----------------------siwuxie095 KO ------- 表中字段名和实体类属性名不一致 如果数据库表中的字段名和实体类的属性名不一致,那么在查询时, 相应字段的结果就 ...

  10. swift UICollectionView使用

    方法1:创建 的时候注册 layout /// 时间view private lazy var timeCollectionV: UICollectionView = { 1.直接注册 并设置好 UI ...