文本文件像这样:
用时间段分割,每个时间段下面有很多tag的行,tag下面又有若干的url行,请问要怎么样把这些数据放到数组中,因为可能需要以时间为索引取出相应的记录(可能要三维数组吧?)在test.txt中:[200508152156]
tag:test1->sub=11
url1=http://www.domain1.com
url2=http://www.domain2.com
tag:test2->sub=22
url3=http://www.domain3.com
url4=http://www.domain4.com
[200508152201]
tag:test3->sub=33
url5=http://www.domain5.com
url6=http://www.domain6.com
tag:test4->sub=44
url7=http://www.domain7.com
url8=http://www.domain8.com
[200508152206]
tag:test5->sub=55
url9=http://www.domain9.com
url10=http://www.domain10.com
tag:test6->sub=66
url11=http://www.domain11.com
url12=http://www.domain12.com我知道要用到BufferedReader类,下面只写了框架,请高手不吝赐教,补充完整!必有重谢!!
在read.jsp中:<%@ page contentType="text/html; charset=gb2312"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%
String srcfile = "E:\\test.txt";BufferedReader br = new BufferedReader(new FileReader(srcfile));
String tmp = br.readLine();
String date = "2005";
int maxLine = 2;while (tmp != null && !tmp.trim().equals("")){ if (tmp.toLowerCase().startsWith("["+ date)){
index ++; }
if(maxLine == index){ if(tmp.toLowerCase().startsWith("tag")){
}
if(tmp.toLowerCase().startsWith("\t"+"url")){
}
}
tmp = br.readLine();
}
用时间段分割,每个时间段下面有很多tag的行,tag下面又有若干的url行,请问要怎么样把这些数据放到数组中,因为可能需要以时间为索引取出相应的记录(可能要三维数组吧?)在test.txt中:[200508152156]
tag:test1->sub=11
url1=http://www.domain1.com
url2=http://www.domain2.com
tag:test2->sub=22
url3=http://www.domain3.com
url4=http://www.domain4.com
[200508152201]
tag:test3->sub=33
url5=http://www.domain5.com
url6=http://www.domain6.com
tag:test4->sub=44
url7=http://www.domain7.com
url8=http://www.domain8.com
[200508152206]
tag:test5->sub=55
url9=http://www.domain9.com
url10=http://www.domain10.com
tag:test6->sub=66
url11=http://www.domain11.com
url12=http://www.domain12.com我知道要用到BufferedReader类,下面只写了框架,请高手不吝赐教,补充完整!必有重谢!!
在read.jsp中:<%@ page contentType="text/html; charset=gb2312"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%
String srcfile = "E:\\test.txt";BufferedReader br = new BufferedReader(new FileReader(srcfile));
String tmp = br.readLine();
String date = "2005";
int maxLine = 2;while (tmp != null && !tmp.trim().equals("")){ if (tmp.toLowerCase().startsWith("["+ date)){
index ++; }
if(maxLine == index){ if(tmp.toLowerCase().startsWith("tag")){
}
if(tmp.toLowerCase().startsWith("\t"+"url")){
}
}
tmp = br.readLine();
}
HashMap map = new HashMap();
map的key放时间,value放tag的list这样就可以了。
ReadPrivateProfileString
HashMap是个好的选择。
就一个结论,正则表达式+HashMap
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReadFile
{
public static void main(String[] args)
{
try
{
FileInputStream input = new FileInputStream("test.txt");
InputStreamReader reader = new InputStreamReader(input);
BufferedReader bfreader = new BufferedReader(reader);
while (true)
{
try
{
String str = bfreader.readLine();
if (str == null)
break;
Pattern p = Pattern.compile("\\[\\d+\\]");
Matcher m = p.matcher(str);
if (m.matches())
{
System.out.println("第一层 --> " + str);
}
Pattern p2 = Pattern.compile("tag");
Matcher m2 = p2.matcher(str); if (m2.find())
{
System.out.println("第二层 --> " + str);
}
Pattern p3 = Pattern.compile("\\surl");
Matcher m3 = p3.matcher(str);
if (m3.find())
{
System.out.println("第三层 --> " + str);
}
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}