参考来自:http://www.cnblogs.com/s3189454231s/p/5626557.html

关于路径的解释:http://blog.csdn.net/bluishglc/article/details/38753047

官方文档:http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResourceAsStream%28java.lang.String%29

0 获取InputStream的几种方式

项目结构:

 //PropertiesWay.class.getResourceAsStream()最终调用是ClassLoader.getResourceAsStream()
1 InputStream inStream1 = PropertiesWay.class.getClassLoader().getResourceAsStream("../resource/test.properties");
InputStream inStream2 = ClassLoader.getSystemResourceAsStream("com/lyh/resource/test.properties");
InputStream inStream3 = new FileInputStream(new File("E:/lyh/file/workspace/ReadPropertiesFile/src/com/lyh/resource/test.properties"));
//servlet环境下还可以用context
//InputStream inStream4 = context.getResourceAsStream("/WEB-INF/config/login.conf");
//InputStream in = context.getResourceAsStream("filePath"); ////通过url获取
//URL url = new URL("path");
//InputStream inStream5 = url.openStream();

关于类名.class.getClassLoader.getSystemResourceAsStreamClassLoader.getSystemResourceAsStream中,路径的解释可以参考:http://blog.csdn.net/bluishglc/article/details/38753047

简而言之就是:

类名.class.getClassLoader.getSystemResourceAsStream(path)中填写的path:以这个类的所在路径(这里是com.lyh.test)为基础。

ClassLoader.getSystemResourceAsStream(path)中填写的path:以classpath的路径(这里是src)为基础。

最重要的是:这里全是相对路径,所以开头不要加“/”。

如果不确定当前的classpath路径,可以通过如下代码获取:

 PropertiesWay.class.getClassLoader().getResource("").toString();

1 通过jdk提供的java.util.Properties类

在操作之前,首先要读取配置文件,有两种方式:load和loadFromXML。

1.1 load

load有两个方法的重载:load(InputStream inStream)、load(Reader reader),所以,可根据不同的方式来加载属性文件。

1.1.1 load(InputStream inStream)

1 p.load(inStream);
2 System.out.println(p.getProperty("name"));
3 p.setProperty("name", "inStream changed");

1.1.2 load(Reader reader)

1.2 loadFromXML

2.通过java.util.ResourceBundle类读取

2.1 通过ResourceBundle.getBundle()

ResourceBundle是一个抽象类,这种方式来获取properties属性文件不需要加.properties后缀名,只需要文件名即可。

 ResourceBundle resource = ResourceBundle.getBundle("com/mmq/test");//test为属性文件名,放在包com.mmq下,如果是放在src下,直接用test即可
2 String key = resource.getString("username");

2.2 从InputStream中读取

获取inputStream的方法和前面load中介绍的一样。

 ResourceBundle resource = new PropertyResourceBundle(inStream); 
2 String key = resource.getString("username");

2017.6.29 java读取.properties配置文件的几种方法的更多相关文章

  1. java读取.properties配置文件的几种方法

    读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的):一.通过jdk提供的java.util.Properties类.此类继承自java.util. ...

  2. 【转载】java读取.properties配置文件的几种方法

    读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的):一.通过jdk提供的java.util.Properties类.此类继承自java.util. ...

  3. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  4. Java 读取 .properties 配置文件的几种方式

    Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中.然而 XML 配置文件需要通过 DOM 或 SAX 方式解析,而读取 properties 配 ...

  5. Java 读取properties 配置文件的几种方式

    基于ClassLoder读取配置文件 Properties properties = new Properties(); // 使用ClassLoader加载properties配置文件生成对应的输入 ...

  6. java读取properties文件的几种方法

    一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取 Properties p=new Properties(); ...

  7. Java 读取 .properties 文件的几种方式

    Java 读取 .properties 配置文件的几种方式   Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中.然而 XML 配置文件需要通过 ...

  8. java读取properties配置文件总结

    java读取properties配置文件总结 在日常项目开发和学习中,我们不免会经常用到.propeties配置文件,例如数据库c3p0连接池的配置等.而我们经常读取配置文件的方法有以下两种: (1) ...

  9. Java 读取clob字段的几种方法

    Java 读取clob字段的几种方法 一.第一种 Clob clob = rs.getClob("remark");//Java.sql.Clob String detailinf ...

随机推荐

  1. 数表( table )

    数表( table ) 题目描述 有一张n×m的数表,其第i行第j列(1≤i≤n,1≤j≤m)的数值为能同时整除i和j的所有自然数之和.给定a,计算数表中不大于a的数之和. 输入 输入包含多组数据. ...

  2. MAC中如何配置两套android-sdk环境

    1 背景介绍 随着android studio的完善和普及,越来越多Android app开发者从Eclipse+ADT环境转到Android Studio(后文统一用AS表示).但是,AS往往需要较 ...

  3. MFC does not support WINVER less than 0x0501 解决方案(转)

    原文转自 http://blog.csdn.net/ygzhong000/article/details/41750841 解决方案:在stdafx.h头文件中添加以下行. #define WINVE ...

  4. 配置wpa_supplicant调试wifi linux下命令行连接wifi

    调试wpa_supplicant先读一下对应的README.txt文件.根据README.txt修改编译配置. wpa_supplicant需要打开以下开关进行编译CONFIG_DRIVER_NL80 ...

  5. 或许你不知道的10条SQL

    一.一些常见的SQL实践 (1)负向条件查询不能使用索引 select * from order where status!=0 and stauts!=1 not in/not exists都不是好 ...

  6. 详解TCP的三次握手四次断开

    本文将分别讲解经典的TCP协议建立连接(所谓的“3次握手”)和断开连接(所谓的“4次挥手”)的过程. 尽管TCP和UDP都使用相同的网络层(IP),TCP却向应用层提供与UDP完全不同的服务.TCP提 ...

  7. hdu 5144(三分+物理)

    NPY and shot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  8. hdu 5109(构造数+对取模的理解程度)

    Alexandra and A*B Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  9. poj 3026(BFS+最小生成树)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12032   Accepted: 3932 Descri ...

  10. hdu 1856(hash+启发式并查集)

    More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) ...