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. eclipse maven install 报错 jdk rather than jre?

    解决方案:window => Perference => java => install jre => 选中jre => edit => 选择文件夹为jdk的

  2. Python小练习之判断一个日期是一年的第几天

    python练手遇到的一个问题写了个统一公式,不用麻烦的分各种类,如果有人测试出错误请评论通知. #分单双月 def dayNum(month,day,isLeap): if month % 2 != ...

  3. Windows Server 2012 NTP时间同步

    非域环境下有外网连接情况的时间同步 1. 打开组策略,Powershell键入命令:gpedit.msc 2. 在计算机策略对话框中,打开如下路径:计算机配置/管理模板/系统/Windows时间服务/ ...

  4. 腾迅云获取免费SSL证书并布置

    上次申请了SSL证书一直没时间布置,今天重新再来操作一次 首先需要申请SSL证书,腾迅云买的域名有免费一年的SSL证书申请,网址:https://console.cloud.tencent.com/s ...

  5. Android开发技术周报183学习记录

    Android开发技术周报183学习记录 教程 Android性能优化来龙去脉总结 记录 一.性能问题常见 内存泄漏.频繁GC.耗电问题.OOM问题. 二.导致性能问题的原因 1.人为在ui线程中做了 ...

  6. Linux服务器开机自动启动服务或脚本的方法

    由于种种原因我们需要重启服务器或断电重启,服务都得手动一个一个启动太过麻烦,所以专门了解开机自启脚本的设置方法. 方式一: 直接在脚本/etc/rc.d/rc.local(和/etc/rc.local ...

  7. PHP的会话控制

    页面数据的作用域: 当前页共享数据:变量.常量 两个页面间传递数据:get,post 跟踪用户的多页面数据共享(会话):session.cookie 全局数据共享:文件.数据库.memcached C ...

  8. python通过手机抓取微信公众号

    使用 Fiddler 抓包分析公众号 打开微信随便选择一个公众号,查看公众号的所有历史文章列表 在 Fiddler 上已经能看到有请求进来了,说明公众号的文章走的都是HTTPS协议,这些请求就是微信客 ...

  9. homebrew 更改镜像,进行成功安装

    在mac系统中,使用homebrew可以很方便的管理包.按照官网的说明执行以下命令时总是报错: /usr/bin/ruby -e "$(curl -fsSL https://raw.gith ...

  10. React Native IOS搭建开发环境

    安装 必须的软件 Homebrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/mast ...