java读取XML文件
1.读取XML文件的类:
public class XMLUtils {
private final String DB_XML_FILE = "/XMLSetting.xml";
public Properties getPropertiesfromXML() {
URL url = XMLUtils.class.getResource(dBXMLFILE);
URI uri;
try {
uri = url.toURI();
InputSource xmlfile = new InputSource(uri.getPath());
MyDefaultHandler handler = new MyDefaultHandler ();
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
SAXParser parser = parserFactory.newSAXParser();
parser.parse(xmlfile, handler);
return handler.getProps();
} catch (ParserConfigurationException e) {
System.out.println(e.getMessage());
return null;
} catch (SAXException e) {
System.out.println(e.getMessage());
return null;
} catch (IOException e) {
System.out.println(e.getMessage());
return null;
} catch (URISyntaxException e) {
System.out.println(e.getMessage());
return null;
}
}
}
2.处理XML文件的类:
import java.util.Properties;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.app.common.Constants;
public class MyDefaultHandler extends DefaultHandler {
private Properties props;
private String key = "";
private StringBuffer value = new StringBuffer();
public MyDefaultHandler() {
props = new Properties();
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
value.append(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
props.put(key, value.toString().trim());
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
value.delete(0, value.length());
key = attributes.getValue(Constants.KEY);
}
public Properties getProps() {
return this.props;
}
}
3.main:
import java.util.Properties;
pu
相关文档:
/**
* 堆排序
*
* */
public void heapSort(double[] a){
double temp;
initCreateHeap(a);
for(int i = a.length - 1; i > 0; i --){
temp = a[0];
a[0] = a[i];
a[i] = temp;
createHeap(a, i, 0);
}
}
/**
* 将数组看成完全二叉树,建立最大堆
* */
private ......
1、利用System.getProperty()函数获取当前路径:
System.out.println(System.getProperty("user.dir"));//user.dir指定了当前的路径
2、使用File提供的函数获取当前路径:
File directory = new File("");//设定为当前文件夹
try{
System.out.println(directory.get ......
package com;
import java.util.*;
public class WhatDay {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
c.setTime(new Date(System.currentTimeMillis()));
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
switch (dayOfWeek) {
ca ......
从本系列前面的文章中,您了解到反射的性能比直接访问要慢许多倍,并了解了用 Javassist 和 Apache
Byte Code Engineering Library (BCEL)进行classworking。Java 顾问 Dennis
Sosnoski 通过演示如何使用运行时 classworking,来用全速前进的生成代码取代反射代码,从而结束他的 Java 编程的动态性
系列。
既然您已经 ......
在JDK1.5之前,对高质量Java多线程并发程序设计时,为防止程序崩掉等现象的出现,比如使用wait()、notify()和synchronized等,需要考虑性能、死锁、公平性、资源管理以及如何避免线程安全性方面带来的危害等诸多因素,通常会采用一些较为复杂的安全策略,加重了程序员的开发负担。在J ......