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();
}
}