源码为:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class regex {
public static void main (String args[]) throws IOException{
byte bt[]=new byte[10000];
File file = new File("C:\\test.txt");
if(!file.exists()){
System.out.println("文件找不到");
}
RandomAccessFile raf = new RandomAccessFile(file, "rw");
System.out.println(file.length());
raf.seek(0);
for(int i=0;i<=raf.length();i++){
bt[i] = raf.readByte();
}
String s = new String(bt);
System.out.print("文件输出"+s);
Pattern p = Pattern.compile("Referer:(.*?)Accept",Pattern.DOTALL);
Matcher m = p.matcher(s);
if (m.find()) {
System.out.println("正则输出:"+m.group(1));
}
else System.out.println("no result");
}
}
C:\\test.txt 内容为:
op=msgcount&charset=gbk&callback=IMOld&refer=hi.baidu.com&un=damoguyan258&.stamp=h4l6m487 HTTP/1.1
Accept: */*
Referer: http://zhidao.baidu.com/question/120610720.html
Accept-Language: zh-CN
Accept-Encoding: gzip, deflate
ThreadID: 5556
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; SE 2.X MetaSr 1.0)
Host: fetch.im.baidu.com
Connection: Keep-Alive
Cookie: BDUSS=Es3RC1QWmQtVi1QUEQxdmVsWS0xan51djBOUzM4ZkJCTVUzdW5qWEZ-endPdXBRQVFBQUFBJCQAAAAAAAAAAAokNw6t8CcOZGFtb2d1eWFuMjU4AAAAAAAAAAAAAAAAAAAAAAAAAACAYIArMAAAAOD6z5YqAAAALWdCAAAAAAAxMC4zNi4xMfDs~E~w7PxPa; BDUT=gggm542C2A08A4ADD4D59B38D1C778B79F7D1386ef17c2a0; BAIDUID=C338BC4650011A75CA5A05D7D2760BB8:FG=1; IM_old=0|h4l6m47x
两个问题:1、读入文件的eofexception怎么解决 2、我想得到Referer:后面的url,可为何正则表达式貌似没有执行一样,没有输出结果
|
|