简单测试java - properties


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

public class TestPorperty {
    /**
     * 写入-properties文件:
     *         conf 传递过来的content里边的字符串
     *         xmlPath是指定的路径
     * */
    public void writeProperty(String conf,String xmlPath) throws Exception{
        Properties prop = new Properties();
        /**
         * 通过"<,>"符号截
         *         prop.setProperty(key,value)
         *         prop.store()写入到传递过来的xmlPath
         * */
        String values[] = conf.split("<,>");
        for (String value : values) {
            int index = value.indexOf("=");
            prop.setProperty(value.substring(0, index),
                    value.substring(index + 1));
            prop.store(new FileOutputStream(xmlPath),
                    xmlPath);
        }
    }
    
    /**
     * 读取:
     *     格式化-properties文件内容为字符串格式
     *         xmlPath是-properties文件路径
     * */
    public void fotmatXml(String xmlPath){
        Properties prop = new Properties();
        StringBuffer content = new StringBuffer();
        String cont = "";
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(
                    xmlPath));
            prop.load(in);
            //返回属性列表中所有键的枚举,如果在主属性列表中未找到同名的键,则包括默认属性列表中不同的键
            Enumeration<?> en = prop.propertyNames();
            while (en.hasMoreElements()) {
                //如果此枚举对象至少还有一个可提供的元素,则返回此枚举的下一个元素
                String key = (String) en.nextElement();
                //用指定的键在此属性列表中搜索属性
                String property = prop.getProperty(key);
                cont = key + "=" + property +"<,>";
                content.append(cont);
            }
            String con = content.substring(0, content.length()-3);
            System.out.println(con);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 1、把字符串写入到-properties文件
     * 2、格式化文件-properties为字符串
     * */
    public static void main(String args[]) throws Exception {  
        Properties prop = new Properties();
        String conf = "";
        String values[] = conf.split("<,>");
        for (String value : values) {
            int index = value.indexOf("=");
            prop.setProperty(value.substring(0, index),
                    value.substring(index + 1));
            prop.store(new FileOutputStream("devoment.properties"),
                    "devoment.properties");
        }

        StringBuffer content = new StringBuffer();
        String cont = "";
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(
                    "devoment.properties"));
            prop.load(in);
            Enumeration<?> en = prop.propertyNames();
            while (en.hasMoreElements()) {
                String key = (String) en.nextElement();
                String property = prop.getProperty(key);
                cont = key + "=" + property +"<,>";
                content.append(cont);
            }
            String con = content.substring(0, content.length()-3);
            System.out.println(con);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }  
}

转载于:https://www.cnblogs.com/love-you-girl/p/3699981.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值