日期:2014-05-20 浏览次数:20820 次
package org.lxh.iodemo.filedemo;
import java.net.*;
import java.io.*;
import java.util.regex.*;
public class URLDemo {
public static void main(String[] args) throws IOException {
String s;
int i = 0;
//网页地址
URL url = new URL("http://money.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/600006.phtml");
//创建输入流
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
//创建输出文档
FileOutputStream fos = new FileOutputStream("D:\\Finance.txt");
//创建输出流
OutputStreamWriter ows = new OutputStreamWriter(fos);
//匹配需要获取的数据的列名
Pattern q = Pattern.compile("((?<=g>)(\\w*?)).*?(?=(</strong>))");
//匹配数据
Pattern p = Pattern.compile("((?<=date=)(\\w*?)).*?(?=('>))|((?<=center\">)(\\d{1,7}?)).*?(?=(</div>))");
String str = null;
//获取源文件内容 匹配 输出
while ((str = br.readLine()) != null) {
//列名
Matcher n = q.matcher(str);
//数据
Matcher m = p.matcher(str);
//提取 列名
while (n.find()) {
//形式转换 成 String
s = String.valueOf(n.group());
//输出到文本文档
ows.write(s + " ");
System.out.printf("%-44s", n.group());
}
//提取 数据
while (m.find()) {
if (i == 0){
System.out.println();
ows.write("\r\n");
}
i++;
s = String.valueOf(m.group());
ows.write(s + " ");
System.out.printf("%-22s", m.group());
//每输出完一行 回车
if (i % 7 == 0)