1.对properties文件的操作:
public class PropUtil {
private static Map<String, String> map = new HashMap<String,String>();
private PropUtil() {
ResourceBundle prop = ResourceBundle.getBundle("config");
Enumeration<String> em = prop.getKeys();
while (em.hasMoreElements()) {
String topic = (String) em.nextElement();
map.put(topic, (String)prop.getObject(topic));
}
}
public static String getValueByTagName(String tagName){
if(map.get(tagName)==null){
new PropUtil();
}
return map.get(tagName);
}
}
import com.yjdgis.database.*;
import com.yjdgis.jcio.JCPGInput;
import com.yjdgis.log.LogFactory;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import org.apache.log4j.Logger;
public class Configuration {
private Properties props;
private FileInputStream ips;
// static Logger LOG = LogFactory.getInstance(Configuration.class);
public Configuration() {
props = new Properties();
}
public Configuration(String filepath) {//加载属性配置文件
props = new Properties();
try {
ips = new FileInputStream(filepath);
props.load(ips);
ips.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 通过属性名获取属性值
* @param key 属性名
* @return
*/
public String getValue(String key) {
if (props.containsKey(key)) {
String value = props.getProperty(key);
return value;
} else {
return "";
}
}
/**
*
* @param key
* @param value
* @param isSave
*/
public void setValue(String filename, String key, String value, boolean isSave) {
if (props.containsKey(key)) {
props.setProperty(key, value);
}
if (isSave) {
FileOutputStream out;
try {
out = new FileOutputStream(filename);
props.store(out, "This file is a envir");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 在指定的属性文件中获取相关属性名的属性值
* @param fileName
* @param key
* @return
*/
public String getValue(String fileName, String key) {
try {
String value = null;
ips = new FileInputStream(fileName);
props.load(ips);
ips.close();
if (props.containsKey(key)) {
value = props.getProperty(key);
return value;
} else {
return value;
}
} catch (Exception e) {
//LOG.info(fileName + ",file doesn't exist!");
e.printStackTrace();
return "";
}
}
public void clear() {
props.clear();
}
}
2、对XML的操作
package com.kmyjd.utils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class XmlUtils {
private static String filePath;
static{
filePath = XmlUtils.class.getClassLoader().getResource("user.xml").getPath();
}
public static Document getDocument() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(new File(filePath));
return document;
}
public static void write2Xml(Document document) throws Exception,
FileNotFoundException{
OutputFormat format = OutputFormat.createCompactFormat();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream(filePath));
writer.write(document);
writer.close();
}
}