views:

91

answers:

5

hi,

Is there a better way to read tokens in a file in java? I am currently using StringTokenizer for splitting the tokens. But it can be quite inefficient in most cases as you have to read token by token.

Thank you

+2  A: 

I think the best and most flexible option for this is Guava's Splitter class. With it, you have a lot of control over how you split a string, and it returns an Iterable<String> of the tokens resulting from a split. You didn't really specify what exactly it is you want to do for which reading token by token is "inefficient", but if you'd prefer a List for example, you could just convert the Iterable to a list using Lists.newArrayList(Iterable) or ImmutableList.copyOf(Iterable).

ColinD
@ColinD this looks nice, I like it +1
ninesided
+2  A: 

You need to add more details, but is simple cases split works quite well.

fastcodejava
+1  A: 

Have a look at this question:

http://stackoverflow.com/questions/3362640/why-are-most-string-manipulations-in-java-based-on-regexp

I think it contains useful information for you.

Niels Basjes
@Niels, fascinating stuff +1
ninesided
+3  A: 

If you look at StringTokenizer in the Java API you will notice that it recommends an alternative:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

If neither of these options suit your needs, you should take a look at Scanner, which also supports pattern matching:

  Scanner scanner= new Scanner(new File("example.txt"));
  while (scanner.hasNextLine()) {
      // do some stuff
  }
ninesided
+2  A: 

I like the StringUtils.split() in Apache's Jakarta classes. It lets you write code like this:

String[] splitStrings = StringUtils.split(unsplitString, "|");

Let's you avoid regex, and it deals with null pointers.

bluedevil2k