|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectkirk.io.FileTokenizer
This class reads a file token by token. Reading files token by token is only useful with text based files.
Methods in this class may generate an IOException.
Construction is done in one of the following ways:
new TokenFileReader(String file);
Most standard. Opens the passed file or throws an IOEception. Uses the
standard delimiters (" ") and any line terminating delimiters, and no
delimiting token returning.
new TokenFileReader(String file, String delims);
Opens the passed file or throws an IOEception. Uses the passed delimiters
and any line terminating delimiters, and no delimiting token returning.
new TokenFileReader(String file, String delims, boolean delimtokens);
Opens the passed file or throws an IOEception. Uses the passed delimiters
and any line terminating delimiters, and delimiting token returning.
new TokenFileReader(String file, boolean delimtokens);
Opens the passed file or throws an IOEception. Uses the standard
delimiters (" ") and any line terminating delimiters, and delimiting
token returning.
Reading the tokens in a file usually goes like this:
FileTokenizer ft = new FileTokenizer("file.txt");
while(ft.fileHasMoreTokens()) {
String token = ft.nextToken();
// Do something with token.
}
Because of the presence of the methods that deal with the current token (see
below), you might want to act on the current token instead.
Below "String token = ft.currentToken();" can be replaced with any of the other
token reading methods.
FileTokenizer ft = new FileTokenizer("file.txt");
while(ft.fileHasMoreTokens()) {
ft.nextToken();
String token = ft.currentToken();
// Do something with token.
}
Be aware that any call to currentToken() before any nextToken() is called after object construction, results in a null result.
Here are the other ways to retrieve the token:
getTokenAsString() will return the same result as currenttoken(). getTokenAsByte() will return the token as an Byte object, or null if that's not possible. getTokenAsCharacter() will return the token as an Character object, or null if that's not possible. getTokenAsDouble() will return the token as an Double object, or null if that's not possible. getTokenAsFloat() will return the token as an Float object, or null if that's not possible. getTokenAsInteger() will return the token as an Integer object, or null if that's not possible. getTokenAsLong() will return the token as an Long object, or null if that's not possible. getTokenAsShort() will return the token as an Short object, or null if that's not possible.
There are also two methods for checking if there are any tokens left.
fileHasMoreTokens() will return true iff the file has more tokens. If !fileHasMoreTokens() then nextToken() will return null. After this call any current token accessors will return null too. lineHasMoreTokens() will return true iff the current line in the file has more tokens.
Two methods are provided for checking on lines.
firsttoken() will return true iff the current token is the first one on the current line. lasttoken() will return true iff the current token is the last one on the current line.
Naturally a method is included to close the file: close().
| Field Summary | |
protected String |
currentline
The current line. |
protected String |
currenttoken
The current token. |
protected String |
delimiters
The tokenizer delimiters. |
protected String |
nextline
The next line in the file. |
protected BufferedReader |
reader
The BufferedReader that wraps the file. |
protected boolean |
returndelims
The return delimiters flag. |
protected StringTokenizer |
tokenizer
The StringTokenizer that is used to tokenize the current line. |
| Constructor Summary | |
FileTokenizer(String file)
Creates a new instance of this class with: file as the file this class works upon. |
|
FileTokenizer(String file,
boolean delimtokens)
Creates a new instance of this class with: file as the file this class works upon and delimtokens to indicate if tokens should be returned. |
|
FileTokenizer(String file,
String delims)
Creates a new instance of this class with: file as the file this class works upon and delims as the tokenizing delimiters. |
|
FileTokenizer(String file,
String delims,
boolean delimtokens)
Creates a new instance of this class with: file as the file this class works upon, delims as the tokenizing delimiters and delimtokens to indicate if tokens should be returned. |
|
| Method Summary | |
void |
close()
Closes the file reader is reading from. |
String |
currentToken()
Returns the current token, or null if there is none. |
boolean |
fileHasMoreTokens()
Returns whether there are more tokens to be read in this file. |
boolean |
firstToken()
Returns whether the current token is the first one on the line. |
Byte |
getTokenAsByte()
Returns the current token as a Byte, or null if there is none/it isn't a Byte. |
Character |
getTokenAsCharacter()
Returns the current token as a Character, or null if there is none/it isn't a Character. |
Double |
getTokenAsDouble()
Returns the current token as a Double, or null if there is none/it isn't a Double. |
Float |
getTokenAsFloat()
Returns the current token as a Float, or null if there is none/it isn't a Float. |
Integer |
getTokenAsInteger()
Returns the current token as an Integer, or null if there is none/it isn't an Integer. |
Long |
getTokenAsLong()
Returns the current token as a Long, or null if there is none/it isn't a Long. |
Short |
getTokenAsShort()
Returns the current token as a Short, or null if there is none/it isn't a Short. |
String |
getTokenAsString()
Returns the current token as a String (which it already is), or null if there is none. |
boolean |
lastToken()
Returns whether the current token is the last one on the line. |
boolean |
lineHasMoreTokens()
Returns whether there are more tokens to be read on this line. |
protected void |
nextLine()
Advances to the next line. |
String |
nextToken()
Returns the next token in the file, or null if there is none. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
protected String currenttoken
protected String currentline
protected String nextline
protected String delimiters
protected boolean returndelims
protected BufferedReader reader
BufferedReader that wraps the file.
protected StringTokenizer tokenizer
StringTokenizer that is used to tokenize the current line.
| Constructor Detail |
public FileTokenizer(String file,
String delims,
boolean delimtokens)
throws IOException
Creates a new instance of this class with:
file as the file this class works upon, delims as the tokenizing delimiters and delimtokens to indicate if tokens should be returned.
The actual FileReader isn't used really, a much nicer class
called BufferedReader is used.
file - The file to be tokenized.delims - The delimiters to be used for tokenizing.delimtokens - Whether to tokenize the delimiters too.StringTokenizer
public FileTokenizer(String file,
String delims)
throws IOException
Creates a new instance of this class with:
file as the file this class works upon and delims as the tokenizing delimiters.The actual
FileReaderisn't used really, a much nicer class calledBufferedReaderis used.This constructor overloads the first one.
file - The file to be tokenized.delims - The delimiters to be used for tokenizing.
public FileTokenizer(String file,
boolean delimtokens)
throws IOException
Creates a new instance of this class with:
file as the file this class works upon and delimtokens to indicate if tokens should be returned.
The actual FileReader isn't used really, a much nicer class
called BufferedReader is used.
This constructor overloads the first one.
file - The file to be tokenized.delimtokens - Whether to tokoenize the delimiters too.
public FileTokenizer(String file)
throws IOException
Creates a new instance of this class with:
file as the file this class works upon.
The actual FileReader isn't used really, a much nicer class
called BufferedReader is used.
This constructor overloads the first one.
file - The file to be tokenized.| Method Detail |
public void close()
throws IOException
IOException
protected void nextLine()
throws IOException
IOException
public String nextToken()
throws IOException
IOExceptionpublic String currentToken()
public String getTokenAsString()
public Byte getTokenAsByte()
public Character getTokenAsCharacter()
public Double getTokenAsDouble()
public Float getTokenAsFloat()
public Integer getTokenAsInteger()
public Long getTokenAsLong()
public Short getTokenAsShort()
public boolean fileHasMoreTokens()
public boolean lineHasMoreTokens()
public boolean firstToken()
public boolean lastToken()
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||