当前位置: 技术问答>java相关
紧急救命:配置文件的读写问题!
来源: 互联网 发布时间:2015-08-23
本文导语: 有配置文件如下格式: [REPORT] # --------------------------------------------------------------------- # directory in which SR "template" files reside Directory = reports [LUT] # --------------------------------------------------------------------- # directory in...
有配置文件如下格式:
[REPORT]
# ---------------------------------------------------------------------
# directory in which SR "template" files reside
Directory = reports
[LUT]
# ---------------------------------------------------------------------
# directory in which LUT files reside
Directory = lut
怎样进行读写操作
[REPORT]
# ---------------------------------------------------------------------
# directory in which SR "template" files reside
Directory = reports
[LUT]
# ---------------------------------------------------------------------
# directory in which LUT files reside
Directory = lut
怎样进行读写操作
|
XML格式的就简单了,只需一个方法,就可以得到节点的名字和值。
例如:
JDOM的例子:
//TestJDOMYS.java
//Use YS.xml
import org.jdom.*;
import java.io.*;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class TestJDOMYS{
public static void main(String args[])throws Exception{
SAXBuilder sb = new SAXBuilder();
//从文件构造一个Document,因为XML文件中已经指定了编码,所以这里不必了
Document doc = sb.build(new FileInputStream("YS.xml"));
//加入一条处理指令
ProcessingInstruction pi = new ProcessingInstruction
("xml-stylesheet","href="/tech-qa-java/bookList.html.xsl" type="text/xsl"");
doc.addContent(pi);
Element root = doc.getRootElement(); //得到根元素
printElements(root,System.out);
System.out.println ("//end");
java.util.List books = root.getChildren(); //得到根元素所有子元素的集合
Element book = (Element)books.get(0); //得到第一个book元素
//得到当前节点下的全面内容
System.out.println ("book:"+book.getContent());
//得到全部子节点的名字
System.out.println ("book get Children:"+book.getChildren());
//得到指定子节点的值
System.out.println ("book get ChildText:"+book.getChildText("price"));
//怎么样,就这么简单
//为第一本书添加一条属性
Attribute a = new Attribute("hot","true");
book.setAttribute(a);
Element author = book.getChild("author"); //得到指定的字元素
author.setText("王五"); //将作者改为王五
//或 Text t = new Text("王五");book.addContent(t);
Element price = book.getChild("price"); //得到指定的字元素
System.out.println ("price:"+price.getText());
//修改价格,比较郁闷的是我们必须自己转换数据类型,而这正是JAXB的优势
price.setText(Float.toString(50.0f));
String indent = " ";
boolean newLines = true;
XMLOutputter outp = new XMLOutputter(indent,newLines,"GBK");
outp.output(doc, new FileOutputStream("YSB.xml"));
}
static void printElements(Element e, OutputStream out) throws IOException, JDOMException {
out.write(("n===== " + e.getName() + ": n").getBytes());
out.flush();
XMLOutputter outputter = new XMLOutputter();
outputter.output(e, out);
out.flush();
/*
for (Iterator i=e.getChildren().iterator(); i.hasNext(); ) {
Element child = (Element)i.next();
printElements(child, out);
}
out.flush();
*/
}
};
现在读取英文格式的XML文件没有问题。
不过中文有乱码
例如:
JDOM的例子:
//TestJDOMYS.java
//Use YS.xml
import org.jdom.*;
import java.io.*;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class TestJDOMYS{
public static void main(String args[])throws Exception{
SAXBuilder sb = new SAXBuilder();
//从文件构造一个Document,因为XML文件中已经指定了编码,所以这里不必了
Document doc = sb.build(new FileInputStream("YS.xml"));
//加入一条处理指令
ProcessingInstruction pi = new ProcessingInstruction
("xml-stylesheet","href="/tech-qa-java/bookList.html.xsl" type="text/xsl"");
doc.addContent(pi);
Element root = doc.getRootElement(); //得到根元素
printElements(root,System.out);
System.out.println ("//end");
java.util.List books = root.getChildren(); //得到根元素所有子元素的集合
Element book = (Element)books.get(0); //得到第一个book元素
//得到当前节点下的全面内容
System.out.println ("book:"+book.getContent());
//得到全部子节点的名字
System.out.println ("book get Children:"+book.getChildren());
//得到指定子节点的值
System.out.println ("book get ChildText:"+book.getChildText("price"));
//怎么样,就这么简单
//为第一本书添加一条属性
Attribute a = new Attribute("hot","true");
book.setAttribute(a);
Element author = book.getChild("author"); //得到指定的字元素
author.setText("王五"); //将作者改为王五
//或 Text t = new Text("王五");book.addContent(t);
Element price = book.getChild("price"); //得到指定的字元素
System.out.println ("price:"+price.getText());
//修改价格,比较郁闷的是我们必须自己转换数据类型,而这正是JAXB的优势
price.setText(Float.toString(50.0f));
String indent = " ";
boolean newLines = true;
XMLOutputter outp = new XMLOutputter(indent,newLines,"GBK");
outp.output(doc, new FileOutputStream("YSB.xml"));
}
static void printElements(Element e, OutputStream out) throws IOException, JDOMException {
out.write(("n===== " + e.getName() + ": n").getBytes());
out.flush();
XMLOutputter outputter = new XMLOutputter();
outputter.output(e, out);
out.flush();
/*
for (Iterator i=e.getChildren().iterator(); i.hasNext(); ) {
Element child = (Element)i.next();
printElements(child, out);
}
out.flush();
*/
}
};
现在读取英文格式的XML文件没有问题。
不过中文有乱码
|
import java.io.*;
import java.util.*;
public class NextConfFile {
// hashconfInfo a Hashtable object to save configure info
Hashtable hashConfInfo;
/**
* construct method
*/
public NextConfFile(){
hashConfInfo = new Hashtable();
}
/**
* read data from a file and write key-value pairs to ClientProperty
* @return true or false
*/
public boolean read() {
// clear Hashtable
hashConfInfo.clear();
// get config filename
String strConfFile;
strConfFile = getFolder() + "\" + getFileName();
try{
// Open config file
FileReader fConfFile = new FileReader(strConfFile);
BufferedReader reader = new BufferedReader(fConfFile);
// read data from config file
String strLine = reader.readLine();
while(strLine != null) {
strLine = strLine.trim();
if(strLine.length()>0 && strLine.charAt(0) != '#') {
String strKey = getKeyPartofLine(strLine);
if(strKey != null)
putProperty(strKey, getValuePartofLine(strLine));
}
// read next line
strLine = reader.readLine();
}
reader.close();
}
catch (java.io.IOException IOE) {
return false;
};
return true;
}
/**
* Get filename from ClientCom.java
* @return Foldername
*/
protected String getFileName() {
//return "next.properties";
return ClientCom.CONF_FILENAME;
}
/**
* Get FolderPath from ClientCom.java
* @return FolderPath
*/
protected String getFolder() {
//return "c:\nhome\next\conf";
return ClientCom.CONF_FOLDER;
}
/**
* put a key-value pair to ClientProperty
*/
protected void putProperty(String key, Object value) {
hashConfInfo.put(key, value);
ClientProperty.getInstance().putProperty(key,value);
}
/**
* get keyword from strLine
* @param strLine a String object which is like "key1 = value1"
* @return key of the strLine or null
*/
private String getKeyPartofLine(String strLine) {
if(strLine == null)
return strLine;
// get the postion of the markchar
int nPos = strLine.indexOf("=");
if(nPos
import java.util.*;
public class NextConfFile {
// hashconfInfo a Hashtable object to save configure info
Hashtable hashConfInfo;
/**
* construct method
*/
public NextConfFile(){
hashConfInfo = new Hashtable();
}
/**
* read data from a file and write key-value pairs to ClientProperty
* @return true or false
*/
public boolean read() {
// clear Hashtable
hashConfInfo.clear();
// get config filename
String strConfFile;
strConfFile = getFolder() + "\" + getFileName();
try{
// Open config file
FileReader fConfFile = new FileReader(strConfFile);
BufferedReader reader = new BufferedReader(fConfFile);
// read data from config file
String strLine = reader.readLine();
while(strLine != null) {
strLine = strLine.trim();
if(strLine.length()>0 && strLine.charAt(0) != '#') {
String strKey = getKeyPartofLine(strLine);
if(strKey != null)
putProperty(strKey, getValuePartofLine(strLine));
}
// read next line
strLine = reader.readLine();
}
reader.close();
}
catch (java.io.IOException IOE) {
return false;
};
return true;
}
/**
* Get filename from ClientCom.java
* @return Foldername
*/
protected String getFileName() {
//return "next.properties";
return ClientCom.CONF_FILENAME;
}
/**
* Get FolderPath from ClientCom.java
* @return FolderPath
*/
protected String getFolder() {
//return "c:\nhome\next\conf";
return ClientCom.CONF_FOLDER;
}
/**
* put a key-value pair to ClientProperty
*/
protected void putProperty(String key, Object value) {
hashConfInfo.put(key, value);
ClientProperty.getInstance().putProperty(key,value);
}
/**
* get keyword from strLine
* @param strLine a String object which is like "key1 = value1"
* @return key of the strLine or null
*/
private String getKeyPartofLine(String strLine) {
if(strLine == null)
return strLine;
// get the postion of the markchar
int nPos = strLine.indexOf("=");
if(nPos