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