快帮帮我吧,不行了,受不了了.搞了几天了.

jljf_hh 2004-04-27 05:03:15
/**
* <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;
}
}
...全文
78 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
nwsl 2004-04-29
  • 打赏
  • 举报
回复
1.不存在http://index.html网址。
2.如果源代码里没有live就读不出东西了。
nwsl 2004-04-29
  • 打赏
  • 举报
回复
String urladdr = "http://index.html";//一个动态变化的页面.
String file2_start = "<live>";
String file2_end = "</live>";
改成
String urladdr = "http://www.ksc.com.cn";//一个动态变化的页面.
String file2_start = "<head>";
String file2_end = "</head>";
试试。
zijibense 2004-04-29
  • 打赏
  • 举报
回复
帮你顶
jljf_hh 2004-04-29
  • 打赏
  • 举报
回复
我想从一个动态网页里取一段内容.

但是在读取动态网页内容时总是读不过去.不知应该如何处理了.
jjja 2004-04-27
  • 打赏
  • 举报
回复
那你给了一堆代码也没说你想干什么丫,让人家怎么帮丫
jljf_hh 2004-04-27
  • 打赏
  • 举报
回复
求求各位帮帮忙吧.

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧