Java解析HTTP文本文件格式
我是java新手,这周刚学过,我的老师想让我解析一个以文本文件格式保存的HTTP请求。基本上他想让我提取Java解析HTTP文本文件格式,java,http,parsing,text,Java,Http,Parsing,Text,我是java新手,这周刚学过,我的老师想让我解析一个以文本文件格式保存的HTTP请求。基本上他想让我提取 只有这一行“GET/fenway/HTTP/1.0\r\n”和这一行“Host:www.redsox.com\r\n” 并将其保存为文本格式。 我的程序还不能正常工作,因为它包含错误。我所做的是逐行读取一个文本文件,然后将其输入一个函数,该函数尝试读取缓冲区并标记每个单词并将其保存到copyGetWord,然后运行while循环检测是否找到单词“GET”,它将getWord存储到arrayl
只有这一行“GET/fenway/HTTP/1.0\r\n”和这一行“Host:www.redsox.com\r\n” 并将其保存为文本格式。 我的程序还不能正常工作,因为它包含错误。我所做的是逐行读取一个文本文件,然后将其输入一个函数,该函数尝试读取缓冲区并标记每个单词并将其保存到copyGetWord,然后运行while循环检测是否找到单词“GET”,它将getWord存储到arraylist 另一件事是,我只允许使用java来完成这个任务,而不允许使用perl或python。我也尝试过jnetpcap库,但是在安装它们时遇到了问题,所以我只能使用文本文件 希望有人能帮忙 这是一个示例文本文件: 传输控制协议,Src端口:bridgecontrol(1073),Dst端口:http(80),Seq:1,Ack:1,Len:270 超文本传输协议 获取/azur/HTTP/1.0\r\n 连接:保持活动状态\r\n 用户代理:Mozilla/4.08[en](WinNT;I)\r\n 主机:www.google.com\r\n
public static void main(String[] args) {
System.out.println("Tries to read the samplepcap1.txt, \n");
ReadingFile1 handle = new ReadingFile1();
ArrayList <String> StoreLine = new ArrayList<String>();
try{
FileReader ReadPcap = new FileReader("samplePcapSmall.txt");
BufferedReader bufferPcap = new BufferedReader(ReadPcap);
String readBufLine = bufferPcap.readLine();
StoreLine.add(readBufLine);
while (readBufLine!=null){
readBufLine = bufferPcap.readLine();
handle.LookForWord(readBufLine);
}
bufferPcap.close();
}
catch (FileNotFoundException e){
System.out.println("\nFile not found");
}
catch (IOException e){
System.out.println("Problem reading the file.\n");
}
}
public String LookForWord(String getWord){
ArrayList <String>MatchingWord = new ArrayList<String>();
StringTokenizer copyGetWord = new StringTokenizer(getWord," ");
while (copyGetWord.hasMoreElements()){
if(copyGetWord.nextToken().matches("GET")){
MatchingWord.add(getWord);
}
}
System.out.println("\nMatch: "+MatchingWord);
return getWord;
}
publicstaticvoidmain(字符串[]args){
System.out.println(“尝试读取samplepcap1.txt,\n”);
ReadingFile1句柄=新的ReadingFile1();
ArrayList StoreLine=新的ArrayList();
试一试{
FileReader ReadPcap=newfilereader(“samplePcapSmall.txt”);
BufferedReader bufferPcap=新的BufferedReader(ReadPcap);
字符串readBufLine=bufferPcap.readLine();
StoreLine.add(readBufLine);
while(readBufLine!=null){
readBufLine=bufferPcap.readLine();
handle.LookForWord(readBufLine);
}
bufferPcap.close();
}
catch(filenotfounde异常){
System.out.println(“\n未找到文件”);
}
捕获(IOE异常){
System.out.println(“读取文件时出现问题。\n”);
}
}
公共字符串LookForWord(字符串getWord){
ArrayList MatchingWord=新的ArrayList();
StringTokenizer copyGetWord=新的StringTokenizer(getWord,“”);
while(copyGetWord.hasMoreElements()){
如果(copyGetWord.nextToken().matches(“GET”)){
MatchingWord.add(getWord);
}
}
System.out.println(“\n匹配:+MatchingWord”);
返回getWord;
}
尝试读取samplepcap1.txt,存储在ArrayList中并显示ArrayList
Match: []
Match: [ GET /fenway/ HTTP/1.0\r\n]
Match: []
Match: []
Match: []
Exception in thread "main" java.lang.NullPointerException
at java.util.StringTokenizer.<init>(StringTokenizer.java:182)
at java.util.StringTokenizer.<init>(StringTokenizer.java:204)
at readingfile1.ReadingFile1.LookForWord(ReadingFile1.java:84)
at readingfile1.ReadingFile1.main(ReadingFile1.java:62)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
匹配:[]
匹配:[GET/fenway/HTTP/1.0\r\n]
匹配:[]
匹配:[]
匹配:[]
线程“main”java.lang.NullPointerException中出现异常
位于java.util.StringTokenizer。(StringTokenizer.java:182)
位于java.util.StringTokenizer。(StringTokenizer.java:204)
在readingfile1.readingfile1.LookForWord(readingfile1.java:84)
在readingfile1.readingfile1.main(readingfile1.java:62)
Java结果:1
生成成功(总时间:1秒)
您的while循环就是问题所在。请尝试以下方法:
String readBufLine = bufferPcap.readLine();
while (readBufLine!=null){
StoreLine.add(readBufLine);
handle.LookForWord(readBufLine);
readBufLine = bufferPcap.readLine();
}
bufferPcap.close();
这将在读取行不为空的情况下从缓冲区和循环中读取行-新行始终作为循环体的最后一个操作读取,以便循环的下一次迭代将在尝试标记读取行之前检查读取行是否为空。while循环是问题所在。请尝试以下方法:
String readBufLine = bufferPcap.readLine();
while (readBufLine!=null){
StoreLine.add(readBufLine);
handle.LookForWord(readBufLine);
readBufLine = bufferPcap.readLine();
}
bufferPcap.close();
这会在读取行不为空的情况下从缓冲区和循环中读取行-新行始终作为循环体的最后一个操作读取,以便循环的下一次迭代将在尝试标记之前检查读取行是否为空。问题是什么?如何处理
NullPointerException
?如果是,您是否检查了什么是空的以及为什么?问题是什么?如何处理NullPointerException
?如果是,您是否检查了什么是空的以及为什么?