??xml version="1.0" encoding="utf-8" standalone="yes"?> JDOM Document<?xml version="1.0" encoding="UTF-8"?> <customers> <customer> <name><occupation>developer</occupation> <!-- comment:following is contact info --> <contact> <email>sinpo.xu@hotmail.com</email> <mobile>15029357227</mobile> <fix-phone>02985457683</fix-phone> </contact> </customer> </customers> <mobile>15029357227</mobile>Element element = new Element("name"); Text text = new Text("AAA.< element.addContent(text); org.w3c.dom.Document
]]>
]]>
package sinpo.usagedemo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
* d配置文gQƈ且修改后?qing)时同步到磁?/font>
* @author 徐辛?sinpo.xu@hotmail.com)
* Oct 23, 2008
*/
public class Configuration {
private Element root = null;
private Document dom = null;
private static final String resourceName = "/config.xml";
private static Configuration _INSTANCE = null;
public static synchronized Configuration getInstance() {
if (_INSTANCE == null) {
_INSTANCE = new Configuration();
}
return _INSTANCE;
}
private Configuration() {
load();
}
public String getConfig(String configName) {
String configValue = null;
Element found = findRecursively(configName, root);
if (found != null) {
configValue = found.getText();
}
return configValue;
}
public void updateConfig(String configName, String newValue)
throws IOException {
Element found = findRecursively(configName, root);
if (found != null) {
found.setText(newValue);
} else {
Element configNode = new Element(configName);
configNode.addContent(newValue);
// also: configNode.setText(newValue);
root.addContent(configNode);
}
sync();
}
public void deleteConfig(String configName) throws IOException {
Element found = findRecursively(configName, root);
if (found != null) {
found.detach();
}
sync();
}
private void load() {
SAXBuilder builder = new SAXBuilder();
InputStream source = getClass().getResourceAsStream(resourceName);
try {
dom = builder.build(source);
root = dom.getRootElement();
} catch (Exception e) {
e.printStackTrace();
}
}
// 递归查找. 在指定的父节点下查找叶子元素
private Element findRecursively(String name, Element parent) {
Element found = null;
List<Element> children = parent.getChildren();
if (children != null) {
for (int i = 0; i < children.size(); i++) {
Element element = children.get(i);
String tmpName = element.getName();
if ((name.equals(tmpName)) && (!hasChild(element))) {
return element;
}
}
for (int i = 0; i < children.size(); i++) {
Element element = children.get(i);
if (hasChild(element)) {
found = findRecursively(name, element);
if (found != null) {
return found;
}
}
}
}
return found;
}
private boolean hasChild(Element element) {
boolean hasChild = false;
List children = element.getChildren();
if ((children != null) && (children.size() > 0)) {
hasChild = true;
}
return hasChild;
}
private void sync() throws IOException {
Format format = Format.getPrettyFormat();
XMLOutputter outputter = new XMLOutputter(format);
File file = null;
URL url = getClass().getResource(resourceName);
if (url == null) {
file = new File(resourceName);
} else {
file = new File(url.getPath());
OutputStream out = null;
try {
out = new FileOutputStream(file);
outputter.output(dom, out);
out.close();
out = null;
} catch (Exception e) {
e.printStackTrace();
if (out != null) {
out.close();
}
}
}
}
}
]]>
synchronized
NIO
package
sinpo.usagedemo;
/**
* 该例子说明线E休眠与{待以及(qing)注意事项?/font>
*
*
@author
徐辛?sinpo.xu@hotmail.com)
* Oct 22, 2008
*/
public class
PendingThreadDemo
{
public
Console console =
new
Console
()
;
private
void
writeToConsole1
() {
synchronized
(
console
){
try
{
Thread.sleep
(
1
*
1000
)
;
//NOTE:sleep时ƈ未释放console别的U程是不能锁定console?/font>
//TODO do things
}
catch
(
InterruptedException e
) {
e.printStackTrace
()
;
}
}
}
private
void
writeToConsole2
() {
synchronized
(
console
){
try
{
console.wait
(
1
*
1000
)
;
//NOTE:wait时别的线E是可以锁定console?/font>
//TODO do things
}
catch
(
InterruptedException e
) {
e.printStackTrace
()
;
}
}
}
}
//控制台类
class
Console
{
//TODO implements me
}
]]>
今天l于扑ֈ一个不错的工具Q能用eclipse{编辑的java代码做成一样风格的html格式Q试用了(jin)挺好用的Q谢?a >Java2Html团队?
]]>
package
sinpo.usagedemo;
import
java.net.DatagramSocket;
import
java.net.InetSocketAddress;
import
java.net.SocketAddress;
import
java.nio.ByteBuffer;
import
java.nio.CharBuffer;
import
java.nio.channels.DatagramChannel;
import
java.nio.channels.SelectionKey;
import
java.nio.channels.Selector;
import
java.nio.charset.Charset;
import
java.util.Iterator;
import
java.util.Set;
/**
*
@author
徐辛?sinpo.xu@hotmail.com) Oct 19, 2008
*/
public class
UDPServer
extends
Thread
{
public
void
run
() {
Selector selector =
null
;
try
{
DatagramChannel channel = DatagramChannel.open
()
;
DatagramSocket socket = channel.socket
()
;
channel.configureBlocking
(
false
)
;
socket.bind
(
new
InetSocketAddress
(
5057
))
;
selector = Selector.open
()
;
channel.register
(
selector, SelectionKey.OP_READ
)
;
}
catch
(
Exception e
) {
e.printStackTrace
()
;
}
ByteBuffer byteBuffer = ByteBuffer.allocate
(
65536
)
;
while
(
true
) {
try
{
int
eventsCount = selector.select
()
;
if
(
eventsCount >
0
) {
Set selectedKeys = selector.selectedKeys
()
;
Iterator iterator = selectedKeys.iterator
()
;
while
(
iterator.hasNext
()) {
SelectionKey sk =
(
SelectionKey
)
iterator.next
()
;
iterator.remove
()
;
if
(
sk.isReadable
()) {
DatagramChannel datagramChannel =
(
DatagramChannel
)
sk
.channel
()
;
SocketAddress sa = datagramChannel
.receive
(
byteBuffer
)
;
byteBuffer.flip
()
;
// 试Q通过收到的ByteBuffer首先通过~省的编码解码成CharBuffer 再输?/font>
CharBuffer charBuffer = Charset.defaultCharset
()
.decode
(
byteBuffer
)
;
System.out.println
(
"receive message:"
+ charBuffer.toString
())
;
byteBuffer.clear
()
;
String echo =
"This is the reply message from 服务器?
;
ByteBuffer buffer = Charset.defaultCharset
()
.encode
(
echo
)
;
datagramChannel.write
(
buffer
)
;
}
}
}
}
catch
(
Exception e
) {
e.printStackTrace
()
;
}
}
}
public static
void
main
(
String
[]
args
) {
new
UDPServer
()
.start
()
;
}
}
package
sinpo.usagedemo;
import
java.net.InetSocketAddress;
import
java.net.SocketAddress;
import
java.nio.ByteBuffer;
import
java.nio.channels.DatagramChannel;
import
java.nio.channels.SelectionKey;
import
java.nio.channels.Selector;
import
java.nio.charset.Charset;
import
java.util.Iterator;
import
java.util.Set;
/**
*
@author
徐辛?sinpo.xu@hotmail.com)
* Oct 19, 2008
*/
public class
UDPClient
extends
Thread
{
public
void
run
() {
DatagramChannel channel =
null
;
Selector selector =
null
;
try
{
channel = DatagramChannel.open
()
;
channel.configureBlocking
(
false
)
;
SocketAddress sa =
new
InetSocketAddress
(
"localhost"
,
5057
)
;
channel.connect
(
sa
)
;
}
catch
(
Exception e
) {
e.printStackTrace
()
;
}
try
{
selector = Selector.open
()
;
channel.register
(
selector, SelectionKey.OP_READ
)
;
channel.write
(
Charset.defaultCharset
()
.encode
(
"Tell me your time"
))
;
}
catch
(
Exception e
) {
e.printStackTrace
()
;
}
ByteBuffer byteBuffer = ByteBuffer.allocate
(
100
)
;
while
(
true
) {
try
{
int
eventsCount = selector.select
()
;
if
(
eventsCount >
0
) {
Set selectedKeys = selector.selectedKeys
()
;
Iterator iterator = selectedKeys.iterator
()
;
while
(
iterator.hasNext
()) {
SelectionKey sk =
(
SelectionKey
)
iterator.next
()
;
iterator.remove
()
;
if
(
sk.isReadable
()) {
DatagramChannel datagramChannel =
(
DatagramChannel
)
sk
.channel
()
;
datagramChannel.read
(
byteBuffer
)
;
byteBuffer.flip
()
;
//TODO 报文{化ؓ(f)RUDP消息q调用RUDP协议处理器来处理
System.out.println
(
Charset.defaultCharset
()
.decode
(
byteBuffer
)
.toString
())
;
byteBuffer.clear
()
;
datagramChannel.write
(
Charset.defaultCharset
()
.encode
(
"Tell me your time"
))
;
}
}
}
}
catch
(
Exception e
) {
e.printStackTrace
()
;
}
}
}
}
]]>
]]>
package sinpo.usagedemo;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import junit.framework.TestCase;
/**
* @author 徐辛?sinpo.xu@hotmail.com)
* Oct 19, 2008
*/
public class LoadResource extends TestCase {
public void test() throws Exception {
//usage 1Q?#160;use absolute path (mostly used)
InputStream in1 = this.getClass().getResourceAsStream("/sinpo/test2.properties");
//usage 2: use relative path
InputStream in2 = this.getClass().getClassLoader().getResourceAsStream("sinpo/test2.properties");
//usage 3: use system class path
InputStream in3 = ClassLoader.getSystemResourceAsStream("system.properties");
//读取的资源作ؓ(f)Properties的输入源
Properties props = new Properties();
props.load(in1);
String propValue = props.getProperty("propKey");
System.out.println(propValue);
//读取的资源作ؓ(f)文本输出
InputStreamReader reader = new InputStreamReader(in1);
BufferedReader bReader = new BufferedReader(reader);
String content = bReader.readLine();
//输出W一行内?/font>
System.out.println(content);
//TODO close them
}
}
]]>