I believe I am not using correctly String Tokenizer. Here is my code:
buffer = new byte[(int) (end - begin)];
fin.seek(begin);
fin.read(buffer, 0, (int) (end - begin));
StringTokenizer strk = new StringTokenizer(new String(buffer),
DELIMS,true);
As you can see I am reading a chunk of lines from a file(end and begin are line numbers) and I am transfering the data to a string tokenizer. My delimitators are:
DELIMS = "\r\n ";
because I want to separate words that have a space between them, or are on the next line.
However this code sometimes separates whole words also. What could be the explanation?? Is my DELIMS string conceived wrong?
Also I am passing “true” as an argument to the tokenizer because I want the delimitators to be treated as tokens as well.( I want this because I want to count the line I am currently at)
Could you please help me. Thanks a lot.
You could always wrap your input stream in a
LineNumberReader. That will keep track of the line number for you. LineNumberReader extendsBufferedReader, which has areadLine()method. With that, you could use a regularStringTokenizerto get your words as tokens. You could use regular expressions orScanner, but for this case, StringTokenizer is simpler for beginners to understand and quicker.You must have a RandomAccessFile. You didn’t specify that, but I’m guessing based on the methods you used. Try something like: