Properties的使用

java 和android Properties的应用,请看main方法


public class PropertiesUtils {

    //根据Key读取Value
    public static String GetValueByKey(String filePath, String key) {
        Properties pps = new Properties();
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(filePath));
            pps.load(in);
            String value = pps.getProperty(key);
            System.out.println(key + " = " + value);
            return value;

        }catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    //读取Properties的全部信息
    public static void GetAllProperties(String filePath) throws IOException {

        Properties pps = new Properties();
        InputStream in = new BufferedInputStream(new FileInputStream(filePath));
        pps.load(in);
        Enumeration en = pps.propertyNames(); //得到配置文件的名字

        while(en.hasMoreElements()) {
            String strKey = (String) en.nextElement();
            String strValue = pps.getProperty(strKey);
            System.out.println(strKey + "=" + strValue);
        }

    }

    //写入Properties信息
    public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException {
        //props.load(context.openFileInput("config.properties"));// 安卓用的
        //OutputStream out = context.openFileOutput("config.properties",Context.MODE_PRIVATE);//安卓用的
        Properties pps = new Properties();
        InputStream in = new FileInputStream(filePath);
        pps.load(in);
        OutputStream out = new FileOutputStream(filePath);
        //下面这段为安卓多出来的。主要是因为Context.Mode造成的。
        Enumeration<?> e = pps.propertyNames();
        if(e.hasMoreElements()){
            while (e.hasMoreElements()) {
                String s = (String) e.nextElement();
                if (!s.equals(pKey)) {
                    pps.setProperty(s, pps.getProperty(s));
                }
            }
        }
        //上面这段为安卓多出来的。主要是因为Context.Mode造成的。
        pps.setProperty(pKey, pValue);
        pps.store(out, "Update " + pKey + " name");
    }

    public static void main(String [] args) throws IOException{
        //WriteProperties("Test.properties","ttt", "333");
        //GetValueByKey("Test.properties","Height");
        GetAllProperties("Test.properties");
    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,061评论 25 709
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,130评论 6 342
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,058评论 19 139
  • 说新年计划好像不恰当,一年的近1/6已经过去,不过好在按旧历还没出正月,也可以说新的一年才开始吧。 一直有许多想做...
    e7830ac221ad阅读 1,400评论 0 0
  • 印章 说罢,老头大袖一挥,凭空出现一道银幕,隐约中看见一个老者,手持拂尘,背背长剑,行走在一片空旷里,“世间本是一...
    Joker_MR阅读 2,571评论 0 0

友情链接更多精彩内容