1、通过类加载器加载

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("Chapter8/test.properties");
Properties p = new Properties();
p.load(inputStream );

2、通过文件系统加载

InputStream inputStream = new FileInputStream("Chapter8/test.properties"); 

以下是获取当前工程路径的方法:

注意:

Class.getResource(String path)

path不以’/'开头时,默认是从此类所在的包下取资源;

path以’/'开头时,则是从ClassPath根下获取;

Class.getClassLoader().getResource(String path)

path不能以’/'开头时;

path是从ClassPath根下获取;

public static void main(String[] args) {
// TODO Auto-generated method stub
String[] path = new String[5];
path[0] = Thread.currentThread().getContextClassLoader()
.getResource("Chapter8/test.properties").getPath();
path[1] = this.getClass().getClassLoader()
.getResource("Chapter8/test.properties").getPath();
path[2] = this.getClass().getResource("/Chapter8/test.properties")
.getPath();
path[3] = this.getClass().getResource("test.properties")
.getPath();
path[4] = ClassLoader.getSystemResource("Chapter8/test.properties")
.getPath();
for (int i = 0; i < path.length; i++) {
System.out.println(path[i]);
}
}

得到的文件路径是:

/D:/Program/Java/NecessaryLearning/bin/Chapter8/test.properties
/D:/Program/Java/NecessaryLearning/bin/Chapter8/test.properties
/D:/Program/Java/NecessaryLearning/bin/Chapter8/test.properties
/D:/Program/Java/NecessaryLearning/bin/Chapter8/test.properties
/D:/Program/Java/NecessaryLearning/bin/Chapter8/test.properties

  

读取properties属性文件的更多相关文章

  1. Java学习笔记——JDBC读取properties属性文件

    Java 中的 properties 文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件. 文件的内容是格式是"键=值"(key-valu ...

  2. 读取properties属性文件——国际化

    public class PropertiesInfo { /** * PropertiesInfo实例 */ private static PropertiesInfo pi = null; pri ...

  3. Java读取利用java.util类Properties读取resource下的properties属性文件

    说明:upload.properties属性文件在resources下 import java.io.IOException;import java.io.InputStream;import jav ...

  4. Code片段 : .properties属性文件操作工具类 & JSON工具类

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “贵专” — 泥瓦匠 一.java.util.Properties API & 案例 j ...

  5. 解决读写properties属性文件

    package com.kzkj.wx.utils; import java.io.BufferedReader; import java.io.File; import java.io.FileIn ...

  6. 章节六、3-读取Properties属性文件

    一.如何读取Properties文件1.创建一个名为ReadingProperties的类 2.创建一个.propertise属性的文件,创建的方式参考“二”中步骤 3.写入如下代码 package ...

  7. java:Properties属性文件概念

    java:Properties属性文件概念 在java之前的国际化程序中提出了一个属性文件的概念,属性文件的后缀是:*.properties,那么在java中提供了意个属性文件的专门操作类,Prope ...

  8. 使用JAVA读写Properties属性文件

     使用JAVA读写Properties属性文件 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数 ...

  9. 集合类——Map集合、Properties属性文件操作

    1.Map集合 Collection集合的特点是每次进行单个对象的保存,若要对一对对象来进行保存就只能用Map集合来保存.即Map集合中一次可以保存两个对象,且这两个对象的关系是key = value ...

随机推荐

  1. ASP.NET MVC最新特性

    问题: 既然我说对ASP.NET MVC很熟很懂,对新技术很感兴趣,那么问题是: 当前最新版MVC是什么?你用的哪个版本?相比前一个版本有什么新的特性? 解析: 在面试的时候,为了争取面试官的好感,显 ...

  2. JSP向后台传递参数的四种方式

    Jsp页面传值的方法 一.通过Form表单提交传值 客户端通过Form表单提交到服务器端,服务器端通过 Java代码 request.getParameter(String xx); 来取得参数(xx ...

  3. LINQ Distinct()

    using System; using System.Collections.Generic; using System.Linq; namespace LinqTest { class Progra ...

  4. Centos7.2 启用iptables

    一.防火墙iptables 简洁介绍 iptables 和 firewalld 都是工作在用户空间.用来定义规则的工具,本身不是防火墙,他们定义的规则,可以让内核空间当中的netfilter读取,并且 ...

  5. 让Win10显示系统中隐藏的文件夹

    让Win10显示系统中隐藏的文件夹.. ------------------------- -----------------------

  6. 利用CoreAnimation实现一个时间的进度条

    (个人原创,转载请注明出处 http://www.cnblogs.com/pretty-guy/p/7460334.html) 在iOS中实现进度条通常都是通过不停的设置progress来完成的,这样 ...

  7. python 三级联动

       china_map ={  "华南":{   "广东":["广州市","佛山市","深圳市", ...

  8. jquery获取json对象中的key小技巧

    jquery获取json对象中的key小技巧 比如有一个json var json = {"name" : "Tom", "age" : 1 ...

  9. [iOS开发]Xcode8兼容iOS7以及低版本Xcode调试高版本iOS系统

    现在的项目一般都要兼容iOS7系统,同时也要兼容iOS10,在Xcode8上面,默认情况下无法调试iOS7,因为缺乏调试iOS7需要的配置文件.同时在低版本的Xcode上面(8以下),也无法调试iOS ...

  10. django源码解析一(请求处理流程)

    1.我们都知道WSGI是一个规范,规范了server和application之间通信的一些约束,server端在监听到请求之后,会把请求转给application去处理,他们之间关联起来的桥梁是一个e ...