Java property配置文件操作

操作代码如下

package com.base.property;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class PropertiesUtil {

    private static Properties prop = new Properties();

    public static void readProperties(String fileName){
        try {
            InputStream in = PropertiesUtil.class.getResourceAsStream(fileName);
            BufferedReader bf = new BufferedReader(new InputStreamReader(in));
            prop.load(bf);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static String getProperty(String key){
        return prop.getProperty(key);
    }

    public static void main(String[] args) {
        PropertiesUtil.readProperties("env.properties");
        System.out.println(PropertiesUtil.getProperty("App"));
        System.out.println(PropertiesUtil.getProperty("Oriente-Agent"));
    }
}

注:
在本例中PropertiesUtil类的同一个目录下,新建env.properties文件,
文件中包含两个配置项App和Oriente-Agent

App=bestfei
Oriente-Agent={"version":"1.0.2.0","os":"6.0","platform":"android"}

如需修改读取配置文件的地址,可修改PropertiesUtil.readProperties,例如

InputStream in = PropertiesUtil.class.getResourceAsStream("/"+fileName);
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,837评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,925评论 6 342
  • 文章图片上传不正常,如需文档,可联系微信:1017429387 目录 1 安装... 4 1.1 配置探针... ...
    Mrhappy_a7eb阅读 6,396评论 0 5
  • 最近疯狂的性格测试,这钱文章是告诫自己莫要沉迷于外界的评测,真正了解自己还是以自己为准,外在测试只能做参考。 1坚...
    澍泽阅读 163评论 1 0
  • vpcs里,按住ctrl 不放,按住c, ping -t 停止 标准ACL应用: 1.VLAN 10和20之间不能...
    搞IT青年小唐唐阅读 839评论 0 0