InputStream is = getClass().getResourceAsStream(path);
Properties dbProps = new Properties();
try
{
dbProps.load(is);
} catch(Exception e) {
System.err.println("Can not read the properties file sure Property");
return "";
}
System.out.print(dbProps.getProperty("Directory"));
你可以判断如果是#就不做
Properties dbProps = new Properties();
try
{
dbProps.load(is);
} catch(Exception e) {
System.err.println("Can not read the properties file sure Property");
return "";
}
System.out.print(dbProps.getProperty("Directory"));
你可以判断如果是#就不做
解决方案 »
- JAVA新手求助
- 看看这个结果怎么出来的
- 请教:一道关于数据库事务的简单笔试题!
- 请教各位达人:一个Java Swing的程序,打成Jar包后执行,如何调用运行另外一个地方的Class类文件?
- 重开一贴继续求教,如何替换掉不是关键字的if
- ---------------socket读取数据的问题--------------
- 请问如何取得当前的年月?
- 时间同步
- 向高手请教:关于framework
- 软件包 java.unit 不存在是什么回事
- 在HP的SERVER上应该装哪个JDK呢?(这台HP是UNIX的操作系统)
- 如何把JBuilder7.0的一个工程内的JApplet移到另一个工程内 ,不损失UI上的东西
还有就是保存问题,
我用prop.store(new FileOutputStream(cfgfile),"testheader");
保存,结果戴中括号的[LUT]此类都在其后加了“=”,
并且两个"Directory"只剩下一个了
不会是很大的文件吧。呵呵<report>
<Directory1>reports</Directory1>
<Directory2>LUT</Directory2>
</report>
通过调用readline方法来读取一行,然后找到你定义的Key,
找到相应的Value.你可以用Map这种数据结构来实现查找功能.别无它法
例如:
JDOM的例子:
//TestJDOMYS.java
//Use YS.xmlimport 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=\"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.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 char
int nPos = strLine.indexOf("=");
if(nPos <= 0)
return null; // get key string
String strLeft = strLine.substring(0, nPos);
strLeft = strLeft.trim(); return strLeft;
} /**
* get value from strLine
* @param strLine a String object which is like "key1 = value1"
* @return value of the strLine or null
*/
private String getValuePartofLine(String strLine) {
if(strLine == null)
return strLine; // get the postion of the char
int nPos = strLine.indexOf("=");
if(nPos <= 0)
return null; // get value string
int nlen = strLine.length();
String strRight = strLine.substring(nPos+1, nlen);
strRight = strRight.trim(); return strRight;
} /**
* Test Method
* read "next.properties" and write the key-value pairs to Console
*/
public static void main(String args[]) { NextConfFile myConfig = new NextConfFile();
myConfig.read();
for (Enumeration e = myConfig.hashConfInfo.keys(); e.hasMoreElements(); ) {
Object key = e.nextElement();
System.out.println(key + "=" + myConfig.hashConfInfo.get(key));
}
}
}