0

谁能给我一个解析文件内容的示例,并在使用 java.util.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) - --------------------> Feed id: 0001 处理

成功。ProcessingTimeTook:5 毫秒。

得到输出,

(FusionResultProcessor.java:145) Feed id: 0001 处理

我只想打印 feed id: xxx 而我不想打印 (fusion...)。那是

我想在分隔符“>”之后打印数据。

4

2 回答 2

0

这是一个使用管道分隔符的示例(未测试):

  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("|");
  }
于 2012-01-06T15:16:28.500 回答
0
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);
}
于 2012-01-06T15:20:10.843 回答