Package hsafx

Class TextInputFile


  • public class TextInputFile
    extends Object
    The TextInputFile class reads text from a file. The class contains the same readLine method as BufferedReader and adds methods to read all the Java primitive data types with integrated error checking. It also handles EOF.
    Version:
    6.0 May 2018
    Author:
    Sam Scott (code cleanup for version 5.0), Tom West
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected boolean closed
      Set if the file has been closed.
      protected static int EMPTY
      Constants and variables used by the input line buffer.
      protected boolean eofFlag
      The end-of-file flag
      protected BufferedReader f
      Stream used for reading input.
      protected String fileName
      The file name.
      protected String lineBuffer  
      (package private) int lineBufferPtr  
      (package private) int ungotChar  
      protected boolean useStandardIO
      Indicator whether to write to standard output or a file.
    • Constructor Summary

      Constructors 
      Constructor Description
      TextInputFile()
      Contructor - TextInputFile to read from standard input.
      TextInputFile​(File file)
      Contructor - TextInputFile to read from a File.
      TextInputFile​(String fileName)
      Contructor - TextInputFile to read from file with specified name.
    • Field Detail

      • fileName

        protected String fileName
        The file name.
      • closed

        protected boolean closed
        Set if the file has been closed.
      • useStandardIO

        protected boolean useStandardIO
        Indicator whether to write to standard output or a file.
      • eofFlag

        protected boolean eofFlag
        The end-of-file flag
      • EMPTY

        protected static final int EMPTY
        Constants and variables used by the input line buffer.
        See Also:
        Constant Field Values
      • lineBuffer

        protected String lineBuffer
      • lineBufferPtr

        int lineBufferPtr
      • ungotChar

        int ungotChar
    • Constructor Detail

      • TextInputFile

        public TextInputFile()
        Contructor - TextInputFile to read from standard input.
      • TextInputFile

        public TextInputFile​(File file)
        Contructor - TextInputFile to read from a File.
        Parameters:
        file - - File to be opened.
      • TextInputFile

        public TextInputFile​(String fileName)
        Contructor - TextInputFile to read from file with specified name.
        Parameters:
        fileName - - Name of the file to be opened.
    • Method Detail

      • close

        public void close()
        Close the file to further reading.
      • eof

        public boolean eof()
        Return whether there is an eof before the next token.
        Returns:
        Whether there is an eof before the next token
      • nextBoolean

        public boolean nextBoolean()
        Read a boolean from the file. The actual text in the file must be either "true" or "false" although case is irrelvant.
        Returns:
        The boolean value read from the file.
      • nextByte

        public byte nextByte()
        Read an 8-bit integer (a "byte") from the file. The actual text in the file must be a number from -128 to 127.
        Returns:
        The byte value read from the file.
      • nextChar

        public char nextChar()
        Read a single character from the file. Note that this discards any whitespace. If you want to get every character on the line, use the readLine () method.
        Returns:
        The character read from the file.
      • nextDouble

        public double nextDouble()
        Read a double precision floating point number (a "double") from the file.
        Returns:
        The double value read from the file.
      • nextFloat

        public float nextFloat()
        Read a floating point number (a "float") from the file.
        Returns:
        The float value read from the file.
      • nextInt

        public int nextInt()
        Read a 32-bit integer (an "int") from the file. The actual text in the file must be a number from -2147483648 to 2147483647.
        Returns:
        The int value read from the file.
      • nextLine

        public String nextLine()
        Read a full line of text from the file.
        Returns:
        The line of text read from the file.
      • nextLong

        public long nextLong()
        Read a 64-bit integer (a "long") from the file.
        Returns:
        The long value read from the file.
      • nextShort

        public short nextShort()
        Read a 16-bit integer (a "short") from the file. The actual text in the file must be a number from -32768 to 32767.
        Returns:
        The short value read from the file.
      • next

        public String next()
        Read a whitespace delimited token from the file.
        Returns:
        The token read from the file.