Java StringTokenizer.nextToken() skips over empty fields

2022-09-03 10:17:42

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();
    ...
    }

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

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


答案 1

There is a RFE in the Sun's bug database about this issue with a status .StringTokenizerWill not fix

The evaluation of this RFE states, I quote:

With the addition of the package in , we have basically obsoleted the need for . We won't remove the class for compatibility reasons. But gives you simply what you need.java.util.regex1.4.0StringTokenizerregex

And then suggests using String#split(String) method.


答案 2

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));
 }