有人能给我一个例子来解析文件的内容,并使用java中的StringTokenizer类在分隔符之后显示该文件中的特定单词吗。
谢谢大家,
实际上我试过这个代码公共类词{
public static void main(String[] args)
{
String line=" ";
int lineNo;
try
{
FileReader fr= new FileReader("f:/parse1.txt");
BufferedReader br=new BufferedReader(fr);
for(lineNo = 1; lineNo <=10; lineNo++)
{
if(lineNo == 2)
{
line = br.readLine();
StringTokenizer st=new StringTokenizer(line,"-->");
while(st.hasMoreTokens())
{
String s=st.nextToken();
System.out.println(s);
}
}else
{
br.readLine();
}
}
}catch(Exception e)
{
System.out.println(e);
}
}
}
我在文本文件中的内容是:
(FusionResultProcessor.java:145)--------------->进纸id:0001正在处理
成功。处理时间:5毫秒。
得到的输出为,
(FusionResultProcessor.java:145)进料id:0001处理
我只想打印提要id:xxx,我不想打印(融合…)。那是
我想在分隔符">"之后打印数据。
下面是一个使用管道分隔符的示例(未测试):
FileInputStream fstream = new FileInputStream("file.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
//Create a StringTokenizer and pass the line to it
StringTokenizer st = new StringTokenizer(strLine);
while(st.hasMoreTokens())
String token = st.nextToken("|");
}
BufferedReader reader = new BufferedReader(new FileReader(new File("/path/to/file.txt")));
StringBuffer stringBuffer = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null) {
stringBuffer.append(line).append("n");
}
StringTokenizer st = new StringTokenizer(stringBuffer.toString());
while (st.hasMoreTokens()) {
String word = st.nextToken();
System.out.println(word);
}