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
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
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)
.
You need to add more details, but is simple cases split works quite well.
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.
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 thesplit
method ofString
or thejava.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
}
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.