原文:http://www.blogjava.net/rochoc/archive/2009/01/09/250741.html
Config.java处理配置文件:
/** *//********************************************************************
* 项目名称 :rochoc<p>
* 包名称 :com.rochoc.autoupdate<p>
* 文件名称 :Config.java<p>
* 编写者 :kfzx-luoc<p>
* 编写日期 :2008-12-22<p>
* 程序功能(类)描述 :<p>
* 用于更新的配置文件读取对像
* 程序变更日期 :
* 变更作者 :
* 变更说明 :
********************************************************************/
package com.rochoc.autoupdate;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/** *//**
* @author kfzx-luoc
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Config
{
public static String cfgFile = ".\\config\\autoupdate.xml";
private static Config config = null;
/** *//** xml的document*/
private Document doc = null;
public static Config getInstance()
{
if(config==null)
{
config = new Config();
}
return config;
}
private Config()
{
try
{
SAXReader reader = new SAXReader();
doc = reader.read(cfgFile);
}catch(Exception e)
{
e.printStackTrace();
}
}
public void refresh()
{
config = new Config();
}
public String getVerstion()
{
if(config==null)
{
return "";
}
List lst = doc.selectNodes("Info/Version");
Element el = (Element)lst.get(0);
return el.getText();
}
public String getServerIp()
{
if(config==null)
{
return "";
}
List lst = doc.selectNodes("Info/UpdateServer/Ip");
Element el = (Element)lst.get(0);
return el.getText();
}
public UpdFile [] getFiles()
{
if(config==null)
{
return null;
}
List file = doc.selectNodes("Info/Files");
List lst = ((Element)file.get(0)).elements();
if(lst.size()==0)
{
return null;
}
UpdFile files[] = new UpdFile[lst.size()];
for(int i=0;i<lst.size();i++)
{
Element el = (Element)lst.get(i);
List childs = el.elements();
Element name = (Element)childs.get(0);//Name
Element path = (Element)childs.get(1);//Path
Element ver = (Element)childs.get(2);//Version
files[i] = new UpdFile(name.getText());
if("File".equals(el.getName()))
{
files[i].setType(0);//文件
}else
{
files[i].setType(1);//目录
}
files[i].setPath(path.getText());
files[i].setVersion(ver.getText());
}
return files;
}
public String getServerPort()
{
if(config==null)
{
return "";
}
List lst = doc.selectNodes("Info/UpdateServer/Port");
Element el = (Element)lst.get(0);
return el.getText();
}
public static void print(String msg)
{
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss->>" );
String str = sdf.format( new Date());
System.out.println(str+msg);
}
public static void main(String args[])
{
Config cfg = Config.getInstance();
UpdFile files[] = cfg.getFiles();
for(int i=0;i<files.length;i++)
{
System.out.println(files[i]);
}
Config.print("test");
}
/** *//**
* 格式化路径,增加其尾部的目录分隔符
*
* @param p 要格式化的目录字符串
*/
public static String formatPath(String p)
{
if (!p.endsWith(File.separator))
return (p + File.separator);
return p;
}
/** *//**
* 格式化路径,去除其尾部的目录分隔符
*
* @param p 要格式化的目录字符串
*/
public static String unformatPath(String p)
{
if (p.endsWith(File.separator))
return (p.substring(0, p.length()-1));
return p;
}
public static byte[] getCmd(String cmd)
{
//第一位用于标识是命令,后面8位为命令名
byte cmdb [] = new byte[9];
cmdb[0] = AUPD.CMD_DATA_SECT;
byte [] tmp = cmd.getBytes();
if(tmp.length!=8)
{
Config.print("命令有误:"+cmd+"<<");
return null;
}
for(int i=0;i<8;i++)
{
cmdb[i+1] = tmp[i];
}
return cmdb;
}
public static String parseCmd(byte cmd[])
{
return new String(cmd,0,8);
}
public static byte [] getLen(int len)
{
String slen = String.valueOf(len);
while(slen.length()<4)
{
slen = "0"+slen;
}
return slen.getBytes();
}
public static void copyArray(byte objary[], byte souary[], int o_begin,
int s_begin, int len)
{
for (int i = 0; i < len; i++)
{
objary[o_begin + i] = souary[s_begin + i];
}
}
}
UpdFile.java:这个类本来是想作为对象直接在服务端和客户端之间传递的,但后来没有采用,因此在本实现中用处不大
/** *//********************************************************************
* 项目名称 :rochoc<p>
* 包名称 :com.rochoc.autoupdate<p>
* 文件名称 :UpdFile.java<p>
* 编写者 :kfzx-luoc<p>
* 编写日期 :2008-12-23<p>
* 程序功能(类)描述 :<p>
* 版本文件对像
* 程序变更日期 :
* 变更作者 :
* 变更说明 :
********************************************************************/
package com.rochoc.autoupdate;
import java.io.Serializable;
/** *//**
* @author kfzx-luoc
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class UpdFile implements Serializable
{
private String name = "";//名称
private String path = "";//路径
private int type = 0;//类型 0.文件 1.目录
private String version = "";//版本号
public UpdFile()
{
super();
}
public UpdFile(String name)
{
this();
this.name = name;
}
public UpdFile(String name,String path,int type,String version)
{
this(name);
this.path = path;
this.type = type;
this.version = version;
}
/** *//**
* @return Returns the name.
*/
public String getName()
{
return name;
}
/** *//**
* @param name The name to set.
*/
public void setName(String name)
{
this.name = name;
}
/** *//**
* @return Returns the path.
*/
public String getPath()
{
return path;
}
/** *//**
* @param path The path to set.
*/
public void setPath(String path)
{
this.path = path;
}
/** *//**
* @return Returns the type.
*/
public int getType()
{
return type;
}
/** *//**
* @param type The type to set.
*/
public void setType(int type)
{
this.type = type;
}
/** *//**
* @return Returns the version.
*/
public String getVersion()
{
return version;
}
/** *//**
* @param version The version to set.
*/
public void setVersion(String version)
{
this.version = version;
}
public String toString()
{
return "Name:"+getName()+",Path:"+getPath()
+",Type:"+getType()+",Version:"+getVersion();
}
}
《CS结构软件自动升级实现》已经全部写完