快帮帮我吧,不行了,受不了了.搞了几天了.
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.net.*;
import java.io.*;
import java.util.Timer;
import java.util.TimerTask;
public class NBAXml {
private final Timer timer = new Timer();
private final int minutes;
private final File file2 = new File("e:\\xmlinfo.xml");
private long start_time = System.currentTimeMillis();
private static NBAXml nbaxml = null;
public NBAXml(int minutes) {
this.minutes = minutes;
}
public void start() {
//定义一个计时任务
TimerTask task = new TimerTask() {
public void run() {
System.out.println("开始" + System.currentTimeMillis());
createFile();
}
//生成XML文件
private void createFile() {
//初始化变量
String urladdr = "http://index.html";//一个动态变化的页面.
String file2_start = "<live>";
String file2_end = "</live>";
String header = "<?xml version='1.0' encoding='gb2312'?><?xml-stylesheet type=\"text/xsl\" href=\"1.xsl\"?> ";
//取得html文件的内容
String htmltxt = getString(urladdr);
if (!htmltxt.equals("")) {
int ifile2_start = htmltxt.indexOf(file2_start);
int ifile2_end = htmltxt.indexOf(file2_end);
if (ifile2_start != -1 && ifile2_end != -1) {
String xmlinfo = htmltxt.substring(ifile2_start, ifile2_end);
try {
PrintWriter writer2 = new PrintWriter(new BufferedWriter(new
FileWriter(file2)));
writer2.write(header + xmlinfo + file2_end);
writer2.close();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
};
timer.schedule(task, 0, minutes * 1000);
}
/**
* args[0]:网址
* args[1]:秒数
* */
public static void main(String args[]) {
nbaxml = new NBAXml(1);
nbaxml.start();
}
public static String getString(String urladdr) {
String ret = "";
URL url = null;
try {
url = new URL(urladdr);
}
catch (MalformedURLException e) {
System.err.println("MalformedURLException:" + e.toString());
//System.exit(1);
}
try {
InputStream ins = url.openStream();
if (ins != null) {
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int len;
while ( (len = ins.read(buffer)) > 0) {
outstream.write(buffer, 0, len);
}
ret = outstream.toString();
outstream.close();
}
}
catch (IOException e) {
System.err.println("IOException:" + e.toString());
//System.exit(1);
}
catch (Exception e) {
System.out.println("Exception:" + e.toString());
}
return ret;
}
}