public abstract class ReadProperties {

     public ReadProperties() {}

     /**
* 回调函数,由调用者处理
* @param key
* @param value
*/
public abstract void dealKeyAndValue(String key, String value); /**
* 根据包路径解析
* @param packagePath
* @throws IOException
*/
public void read(String packagePath) throws IOException {
InputStream is = this.getClass().getResourceAsStream(packagePath);
read(is);
} /**
* 根据文件的绝对路径解析
* @param absolutePath
* @throws IOException
*/
public void readFile(String absolutePath) throws IOException {
read(new File(absolutePath));
} /**
* 根据{@link File}解析
* @param file
* @throws IOException
*/
public void read(File file) throws IOException {
read(new FileInputStream(file));
} /**
* 根据{@link InputStream}解析
* @param is
* @throws IOException
*/
public void read(InputStream is) throws IOException {
Properties properties = new Properties();
try {
// Properties文件会出现乱码问题,以UTF-8的方式打开
properties.load(new InputStreamReader(is, "UTF-8"));
Enumeration<Object> keys = properties.keys(); while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String value = properties.getProperty(key);
properties.get(key); dealKeyAndValue(key, value);
}
} finally {
is.close();
}
} }

使用:
在src下新建一个test.properties文件如下:

执行解析:

 public class Test {

     public static void main(String[] args) throws Exception {
new ReadProperties() {
@Override
public void dealKeyAndValue(String key, String value) {
System.out.println(key + " = " + value);
}
}.read("/test.properties");;
} }

结果如下:

【Java】Properties文件的解析的更多相关文章

  1. Java Properties集合基础解析

    Java Properties集合基础解析 本期学习的properties集合是项目中经常用到的操作 什么是Properties集合? java.util.Properties集合继承于Hashtab ...

  2. java properties文件转义字符和中文乱码解决

    properties文件的分隔符是   =或者 : 第一次出现的就是分割符,第二次出现的也不需要转义,也即是(忽略掉[],只是着重描述字符) [\=]     [\:]   或者  [=]  [:] ...

  3. properties文件的解析

    此文章是从网上看到一篇实用小文章,感觉不过,摘录下来的!如有问题,可及时联系,可立刻做相应处理! Java读取.properties 配置文件的几种方法 在做java工程时, 经常会将一些配置信息放到 ...

  4. java properties 文件中书写相对路径

    工程src下的properties 文件要引用发布到D:\work\apache-tomcat-7.0.52\webapps\项目名称\certs这个地址下的文件,properties 中的文件路径应 ...

  5. log4j.properties文件无法解析

    普通工程:log4j.properties文件必须放在src根目录下

  6. log4j 日志脱敏处理 + java properties文件加载

    Java 加载Properties 配置文件: ResourceBundle bundle = ResourceBundle.getBundle("log4j_filter"); ...

  7. Java properties文件用法

    package com.suyang.properties; import java.io.FileInputStream; import java.io.FileNotFoundException; ...

  8. 自定义Yaml解析器替换Properties文件

    自定义Yaml解析器替换Properties文件 项目结构 案例代码 配置类SpringConfiguration @Configuration @Import(JdbcCofnig.class) @ ...

  9. java解析properties文件

    在自动化测试过程中,经常会有一些公用的属性要配置,以便后面给脚本使用,我们可以选择xml, excel或者json格式来存贮这些数据,但其实java本身就提供了properties类来处理proper ...

随机推荐

  1. ubuntu下file_get_contents返回空字符串

    ubuntu下file_get_contents返回空字符串 | 浏览:302 | 更新:2014-03-30 10:11 本文起初面临的问题是PHP中SoapClient不好使,最后file_get ...

  2. weexpack打包weex项目运行/打包记录

    构建weex项目 安装weex-toolkit cnpm install -g weex-toolkit 初始化一个项目只需新建文件夹并在目录下执行 weex init 即可 安装依赖:cnpm in ...

  3. 51nod OJ P1000 A+B

    P1000 A+B OJ:51Nod 链接:"http://www.51nod.com/Challenge/Problem.html#!#problemId=1000" 题目描述: ...

  4. pip 安装模块时出现error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools":

    在使用pip安装mysqlclient模块时,出现如下错误: 在网上查找资料后显示可能是由于不兼容导致的,最好去下载.whl文件安装成功. 资源地址:http://www.lfd.uci.edu/~g ...

  5. Go语言数组

    目录 声明数组 数组在内存的结构 初始化数组 使用数组 函数间传递数组 数组使用注意事项 数组是具有相同唯一类型的一组已编号且长度固定的数据项序列(这是一种同构的数据结构);这种类型可以是任意的原始类 ...

  6. 如何批量添加图片到ppt的方法

    如何批量添加图片到ppt 许多时候会做一些幻灯片,需要大量的图片,但是往往一张以张的加图片,会很浪费时间,如何快速添加图片,一次解决呢? 步骤:插入-相册-点击相册 点击文件,批量选择你要插入的图片, ...

  7. JavaScript递归中的作用域问题

    需求是这样的,从子节点寻找指定className的父节点,一开始就想到递归(笨!),Dom结构如下: <div class="layer_1"> <div cla ...

  8. State状态模式

    1.简介 在日常开发中,某些对象的状态如果发生改变,对应的行为也将发生改变,那么如何在运行时根据对象的状态动态的改变对象的行为,同时不产生紧耦合关系(即使用if else或者swith所带来的紧耦合关 ...

  9. Spring boot集成spring-boot-starter-data-jpa环境搭建

    1.创建Spring boot项目 2.保存等待构建完成 3.增加spring-boot-starter-data-jpa.内存数据库依赖包hsqldb <!-- 添加data jpa依赖 -- ...

  10. mysql 开发基础系列17 存储过程和函数(上)

    一. 概述 存储过程和函数是事先经过编译并存储在数据库中的一段sql语句集合,可以简化应用开发人员的很多工作,减少数据在数据库与应用服务器之间的传输,提高数据处理效率是有好处的.存储过程和函数的区别在 ...