StringTokenizer错误地使用行作为标记

时间:2014-03-28 02:49:43

标签: java stringtokenizer

所以我的代码到目前为止已经对数据进行了标记化,但它似乎将标记显示为整行文本而不是我希望得到的单个子字符串

我的代码:

List<Flow> parseData(String filename) throws NumberFormatException, IOException
{
    List<Flow> fl = new ArrayList<Flow>();
    BufferedReader reader = new BufferedReader(new FileReader(filename));
    String line = null;
    while ((line = reader.readLine()) != null) 
    {
            String data = line.replaceAll("->", "");
            String delim = "\t";
            StringTokenizer tok = new StringTokenizer(data, delim, true);

        while (tok.hasMoreTokens()) 
        {

            String token = tok.nextToken();
            System.out.println(token + "\n");
            String [] toKen = new String [9];

            if (delim.equals(token)) 
            {

                while(data.length() != 0)
                {
                    for (int i=0; i<9; i++)
                    {
                        toKen[i] = token;
                        System.out.println(toKen[i]);
                    }

                    double dur = Double.parseDouble(toKen[3]);
                    int pkts = Integer.parseInt(toKen[6]);
                    String Date = toKen[1].toString()+" "+toKen[2].toString();
                    Flow flow = new Flow(id,Date,dur,toKen[3],toKen[4],toKen[5],pkts,toKen[7],toKen[8]);
                    fl.add(flow);
//                      System.out.println(flow.toString());
                        id++;

                    }
            }
        }
    }

        return fl;

}

因此它将以下内容显示为单个标记:

2014-03-24 19:11:42.838  7611.668 UDP       192.168.0.15:5353       224.0.0.251:5353        53     5353    12

我更喜欢令牌看起来像这样:

UDP
192.168.0.15:5353
224.0.0.251:5353

1 个答案:

答案 0 :(得分:0)

试试这个:

String input = "2014-03-24 19:11:42.838  7611.668 UDP       192.168.0.15:5353       224.0.0.251:5353        53     5353    12";
String[] tokens = input.split("\\s+");
System.out.println(tokens[3]);
System.out.println(tokens[4]);
System.out.println(tokens[5]);