Java StringTokenizer.nextToken()跳过空字段 [英] Java StringTokenizer.nextToken() skips over empty fields

查看:107
本文介绍了Java StringTokenizer.nextToken()跳过空字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用制表符(/t)作为分隔符,并且我知道我的数据中有一些空白字段,例如:

I am using a tab (/t) as delimiter and I know there are some empty fields in my data e.g.:

one->two->->three

其中->等于制表符.如您所见,空白字段仍然正确地被制表符包围.使用循环收集数据:

Where -> equals the tab. As you can see an empty field is still correctly surrounded by tabs. Data is collected using a loop :

 while ((strLine = br.readLine()) != null) {
    StringTokenizer st = new StringTokenizer(strLine, "\t");
    String test = st.nextToken();
    ...
    }

但是Java会忽略此空字符串",并跳过该字段.

Yet Java ignores this "empty string" and skips the field.

是否有一种方法可以避免这种行为,并迫使Java无论如何都要读取空字段?

Is there a way to circumvent this behaviour and force java to read in empty fields anyway?

推荐答案

完全谢谢.由于第一条评论,我找到了一个解决方案:是的,您是对的,感谢您的参考:

Thank you at all. Due to the first comment I was able to find a solution: Yes you are right, thank you for your reference:

 Scanner s = new Scanner(new File("data.txt"));
 while (s.hasNextLine()) {
      String line = s.nextLine();
      String[] items= line.split("\t", -1);
      System.out.println(items[5]);
      //System.out.println(Arrays.toString(cols));
 }

这篇关于Java StringTokenizer.nextToken()跳过空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆