配置文件位置及内容









执行结果




程序代码


  1. package Utils.ConfigFile;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedReader;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.util.Enumeration;
  10. import java.util.Iterator;
  11. import java.util.Map;
  12. import java.util.Properties;
  13.  
  14. public class PropertiesTest {
  15.  
  16.     /**
  17.      * 获取属性文件
  18.      * @param path  属性文件路径
  19.      * @return
  20.      */
  21.     public static Properties getPropsFile(String path) {
  22.         Properties props = new Properties();
  23.         try {
  24.             File file = new File(path);
  25.             InputStream in = new BufferedInputStream(new FileInputStream(file));
  26.  
  27.             //解决中午乱码问题--因为字节流无法读取中文,所以采用reader把inputStream转换成reader用字符流来读取中文
  28.             BufferedReader bf = new BufferedReader(new InputStreamReader(in));
  29.             props.load(bf);
  30.             in.close();
  31.         } catch (Exception e) {
  32.             return null;
  33.         }
  34.         return props;
  35.     }
  36.  
  37.     /**
  38.      * 显示所有键值
  39.      * @param properties
  40.      */
  41.     public static void showKeys(Properties properties){
  42.         Enumeration<?> enumeration = properties.propertyNames();
  43.  
  44.         System.out.println("======下面将显示所有key值============");
  45.         while(enumeration.hasMoreElements()){
  46.             Object key = enumeration.nextElement();
  47.             System.out.println(key);
  48.         }
  49.     }
  50.  
  51.     /**
  52.      * 显示所有value值
  53.      * @param properties
  54.      */
  55.     public static void showValues(Properties properties){
  56.         Enumeration<?> enumeration = properties.elements();
  57.  
  58.         System.out.println("======下面将显示所有value值============");
  59.         while(enumeration.hasMoreElements()){
  60.             Object value = enumeration.nextElement();
  61.             System.out.println(value);
  62.         }
  63.     }
  64.  
  65.     /**
  66.      * 显示所有key,value
  67.      * @param properties
  68.      */
  69.     public static void showKeysAndValues(Properties properties){
  70.         Iterator<Map.Entry<Object, Object>> it= properties.entrySet().iterator();
  71.  
  72.         System.out.println("======下面将显示所有<key,value>值---方式1============");
  73.         while (it.hasNext()) {
  74.             Map.Entry<Object, Object> entry = it.next();
  75.             Object key = entry.getKey().toString();
  76.             Object value = entry.getValue();
  77.             System.out.println("<" + key + "," + value + ">");
  78.         }
  79.     }
  80.  
  81.     /**
  82.      * 显示所有key,value
  83.      * @param properties
  84.      */
  85.     public static void showKeysAndValues2(Properties properties){
  86.         System.out.println("======下面将显示所有<key,value>值--方式2============");
  87.         for (Map.Entry<Object, Object> entry: properties.entrySet()) {
  88.             Object key = entry.getKey();
  89.             Object value = entry.getValue();
  90.             System.out.println("<" + key + "," + value + ">");
  91.         }
  92.     }
  93.  
  94.     public static void main(String args[]) {
  95.         Properties propFile = getPropsFile("C:\\myProperties.properties");
  96.  
  97.         showKeys(propFile);
  98.         showValues(propFile);
  99.         showKeysAndValues(propFile);
  100.         showKeysAndValues2(propFile);
  101.     }
  102. }

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 





【Properties文件】Java使用Properties来读取配置文件的更多相关文章

  1. JAVA使用相对路径读取配置文件

    JAVA使用相对路径读取配置文件[align=center][/align][size=medium][/size]   在软件开发中经常遇到读取配置文件,以及文件定位问题.今天做个总结.   (一) ...

  2. 读取properties文件------servletcontext及dao层读取

    用servletcontext读取properties文件-------1) 重点在于:InputStream in=this.getServletContext().getResourceAsStr ...

  3. 利用Properties属性集结合类加载器读取配置文件

    配置文件test.properties a=123 测试类Demo1.java public class Demo1 { public static void main(String[] args) ...

  4. 如何读取jar包外的properties文件和log4j.properties

    http://jrails.iteye.com/blog/1705464 ***************************************' 一般在项目中使用properties配置文件 ...

  5. 将properties文件放在Jar包并读取

    有时候需要在一个library内部打包一个properties文件,包含一些配置信息,而不能部署在外部. 在maven工程里面,将properties文件放在src/main/resources目录下 ...

  6. Java工程中如何读取配置文件中参数信息

    Java中读取配置文件中参数: 方法一:通过JDK中Properties来实现对配置文件的读取. Properties主要用于读取Java的配置文件,不同的编程语言有自己所支持的配置文件,配置文件中很 ...

  7. java 4种方式读取配置文件 + 修改配置文件

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 方式一采用ServletContext读取读取配置文件的realpath然后通过文件流读取出来 方式二采用ResourceB ...

  8. java读取package中的properties文件java.util.MissingResourceException

    文件结构: /build/classes/d914/Hello.class /build/classes/d914/mess.properties /build/classes/d914/mess_z ...

  9. Java读取Properties文件 Java加载配置Properties文件

    static{ Properties prop = new Properties(); prop.load(Thread.currentThread().getContextClassLoader() ...

  10. java web编程 servlet读取配置文件参数

    新建一个servlet. 然后在web.xml文件里面自动帮助你创建好了<servlet-name><servlet-class><servlet-mapping> ...

随机推荐

  1. cocos2d-html5版日历组件

    根据一as3版本给改编成成了js版的.as3版本的地址:  http://download.csdn.net/detail/fengye513/2722289 胜利项目做的差不多了,由于项目用的是c+ ...

  2. sql server中NULL导入decimal字段时报错

    sql server中NULL导入decimal字段时报错 在导入CSV文件时,如果decimal字段为null值,导致文本文件入库时失败. 错误现象 构造例子 新建一张表,包含decimal字段. ...

  3. hdu 5206 Four Inages Strategy

    题目大意: 判断空间上4个点是否形成一个正方形 分析: 标称思想 : 在p2,p3,p4中枚举两个点作为p1的邻点,不妨设为pi,pj,然后判断p1pi与p1pj是否相等.互相垂直,然后由向量法,最后 ...

  4. c 函数及指针学习 9

    指针的高级应用 处理命令行参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <stdio.h>   int main(int ar ...

  5. Ubuntu 安装软件的命令

    Ubuntu 安装软件的命令 安装flashplayer sudo apt-get install flashplugin-installer 安装百度云客户端 软件在百度云盘里面 安装必要的开发环境 ...

  6. MySQL二进制日志的备份和恢复

    二进制日志:记录数据库修改的相关操作,作用是即时点回复,主从复制 可以按时间滚动,也可以按大小滚动 server-id:服务器身份标识 一.二进制文件的删除方法,千万不要手动删除 PURGE BINA ...

  7. C/C++程序员必须熟练应用的开源项目(转-清风小阁)

    作为一个经验丰富的C/C++程序员, 肯定亲手写过各种功能的代码, 比如封装过数据库访问的类, 封装过网络通信的类,封装过日志操作的类, 封装过文件访问的类, 封装过UI界面库等, 也在实际的项目中应 ...

  8. hive权限管理之实践

    一.实践心得 主要参考这个连接,里面说得也挺详细的.http://www.aboutyun.com/thread-12549-1-1.html 总结如下: 1.若赋予用户某个表的权限,查用户在该表所属 ...

  9. LeetCode() Search a 2D MatrixII

    一开始的超时思路 int row=a.size(),col=a[0].size(); for(int i=0;i<row;i++) { if(a[i][col-1] > target &a ...

  10. HDU-1828 Picture(扫描线)

    题目大意:给若干个矩形,求轮廓边长. 题目分析:与求面积类似.按从下往上扫描,仍然是底边添加,上边删除.但要同时维护竖边的数目,每次扫描对答案的贡献为扫描线上总覆盖长度的变化量加上竖边的增量.总覆盖长 ...