properties的配置添加、删除、修改操作

package com.zte.fox.metric.utils;


import com.zte.fox.metric.property.SafePropert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.w3c.dom.Entity;


import java.io.*;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;




@Component
public class PropertiesUtils {




    private static final Logger log = LoggerFactory.getLogger(PropertiesUtils.class);


    private static String profilepath = "/Application-#.properties";


    private static Properties props = new Properties();


    private static String env =  System.getProperty("spring.profiles.active","pro");
    public PropertiesUtils() {


        profilepath = profilepath.replace("#",env);
        log.info("-----------profilepath:{}", profilepath);
        InputStream is = null;
        try {
            if(env.equals("local")){
                is = this.getClass().getResourceAsStream(profilepath);
            }else{
                String filePath = System.getProperty("user.dir")+profilepath;
                is = new BufferedInputStream(new FileInputStream(filePath));
            }


                props.load(is);
        } catch (Exception e) {
            log.error("PropertiesUtils--", e);
            return;
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    log.error("PropertiesUtils-close-", e);
                }
            }
        }


    }


    public String getKeyValue(String key) {
        return props.getProperty(key);
    }




    /**
     *
     * @param keyname
     * @param keyvalue
     * @param isAppend :是否追加
     */
    public void writeProperties(String keyname, String keyvalue,boolean isAppend) {
        OutputStream fos = null;
        try {
            String path = "";
            if(env.equals("local")){
                path =  this.getClass().getResource(profilepath).getPath();
            }else{
                path = System.getProperty("user.dir")+profilepath;
            }


            log.info("-writeProperties-path:{}",path);
            File file = new File(path);
            if(file.exists()){
                log.info("exist:" + file.getAbsolutePath());
            }else{
                log.info("{} not exist", path);


            }
            fos = new FileOutputStream(file);
            if(isAppend){
                String v = props.get(keyname).toString();
                keyvalue =v+","+keyvalue;
            }
            props.setProperty(keyname, keyvalue);


            props.store(fos, "Update '" + keyname + "' value");
        } catch (IOException e) {
            log.error("writeProperties:",e);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                   log.error("writeProperties:",e);
                }


            }
        }
    }




    public void removeProperties(String keyname) {
        OutputStream fos = null;
        Properties properties = new Properties();


        try {
            String path = "";
            if(env.equals("local")){
                path =  this.getClass().getResource(profilepath).getPath();
            }else{
                path = System.getProperty("user.dir")+profilepath;
            }
            File file = new File(path);
            if(file.exists()){
                log.info("exist:" + file.getAbsolutePath());
            }else{
                log.info("{} not exist", path);


            }
            fos = new FileOutputStream(file);
            Enumeration enumeration = props.propertyNames();
            while(enumeration.hasMoreElements() ){
                String key = (String)enumeration.nextElement();
                if(key.equals(keyname)){
                    continue;
                }
                properties.setProperty(key,props.getProperty(keyname));


            }


            properties.store(fos, "Update '" + keyname + "' value");
            writeProperties(keyname,"",false);
        } catch (IOException e) {
            log.error("writeProperties:",e);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }
    }


}


public void saveProperties()  
    {  
        try  
        {  
            Properties properties = new Properties();  
  
            Properties p = new Properties();  
            File file = new File("F://test.properties");  
            p.load(new FileInputStream(file));  
            Set<String> pSet = p.stringPropertyNames();  
            Iterator i = pSet.iterator();  
            while(i.hasNext())  
            {  
                String propertiesName = i.next().toString();  
                //删除一个当获取的名称hk相同时,就返回到下一步;break;是退出循环  
                if("hk".equalsIgnoreCase(propertiesName)) continue;  
                properties.setProperty(propertiesName, p.getProperty(propertiesName));  
                //修改  
                if("japan".equalsIgnoreCase(propertiesName))  
                {  
                    properties.setProperty(propertiesName, "123456789");  
                }  
            }  
            properties.setProperty("usa", "美国");      
            properties.setProperty("hk", "香港");   
            properties.setProperty("japan", "日本");                
            properties.setProperty("china", "中国");    
                                                //添加              
            properties.store(new FileOutputStream(file), properties.toString());  
              
        } catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
    }  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值