statalign.io.input
Class FileTokenReader

java.lang.Object
  extended by statalign.io.input.FileTokenReader

public class FileTokenReader
extends java.lang.Object

Class that allows reading the contents of a file token by token, without worrying about line ends and whitespace. E.g. files containing ten integers in a single line or ten lines can be handled in a uniform way. Inspired by the C call fscanf(file, "%d", &value). Tokens can be integers, doubles or strings. Separators are any combination of whitespace characters as recognized by the regular expression \s+ .

Author:
novak

Constructor Summary
FileTokenReader(java.lang.String fname)
          Constructs a FileTokenReader reading a given file.
 
Method Summary
 void close()
          Closes the underlying file.
 java.lang.Double readDbl()
          Reads the next token and tries to interpret it as a double value.
 java.lang.Integer readInt()
          Reads the next token and tries to interpret it as an integer value.
 java.lang.String readStr()
          Reads the next token and returns it as a String.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileTokenReader

public FileTokenReader(java.lang.String fname)
                throws java.io.IOException
Constructs a FileTokenReader reading a given file.

Parameters:
fname - Name of the file to read
Throws:
java.io.IOException - if an I/O error occurs
Method Detail

readDbl

public java.lang.Double readDbl()
                         throws java.io.IOException
Reads the next token and tries to interpret it as a double value.

Returns:
Double value of the token or null if end of file has been reached
Throws:
java.io.IOException - if an I/O error occurs
java.lang.NumberFormatException - if next token cannot be converted to Double

readInt

public java.lang.Integer readInt()
                          throws java.io.IOException
Reads the next token and tries to interpret it as an integer value.

Returns:
Integer value of the token or null if end of file has been reached
Throws:
java.io.IOException - if an I/O error occurs
java.lang.NumberFormatException - if next token cannot be converted to Integer

readStr

public java.lang.String readStr()
                         throws java.io.IOException
Reads the next token and returns it as a String.

Returns:
Next token as String or null if end of file has been reached
Throws:
java.io.IOException - if an I/O error occurs

close

public void close()
           throws java.io.IOException
Closes the underlying file.

Throws:
java.io.IOException - if an I/O error occurs