Michael Thomas Flanagan's Java Scientific Library

FileInput Class:      Reading from a File

     

Last update: 20 May 2012                                                                                                                              Main Page of Michael Thomas Flanagan's Java Scientific Library

This class contains easy to use methods for reading numbers and characters, of the following types, from a text file:
  • floating point, double;
  • floating point, float;
  • arbitrary precision, BigDecimal;
  • complex number, Complex;
  • phasor, Phasor;
  • word, a String terminated by a space;
  • line, a String terminated by a new line return;
        
  • integer, int;
  • long integer, long;
  • big integer, BigInteger;
  • short integer, short;
  • byte integer, byte;
  • boolean, boolean;
  • isolated character, char
    i.e. separated by spaces, tabs, commas, colons or semicolons.
and methods for returning the number of lines in the file, the file name, the file path and copying the file.

This class is a superclass for the subclass FileChooser which allows the input file to be selected via a dialog box and which inherits all the read and get methods of this class.

See FileManipulate for a class containing methods enabling a file to be read in as a stream of char, Char or the ISO code int equivalents.

import directive: import flanagan.io.FileInput;
Related classes:

SUMMARY OF CONSTRUCTOR AND METHODS

Constructor public FileInput(String filename)
Read a double floating point number (double) public final synchronized double readDouble()
Read a float floating point number (float) public final synchronized float readFloat()
Read a BigDecimal number (BigDecimal) public final synchronized BigDecimal readBigDecimal()
Read an integer (int) public final synchronized int readInt()
Read a long integer (long) public final synchronized long readLong()
Read a big integer (BigInteger) public final synchronized BigInteger readBigInteger()
Read a short integer (short) public final synchronized short readShort()
Read a byte integer (byte) public final synchronized byte readByte()
Read a separated character (char) public final synchronized char readChar()
Read a word of text (String) public final synchronized String readWord()
Read a word of text separated only by spaces (String) public final synchronized String readWordSpaceOnly()
Read a line of text (String) public final synchronized String readLine()
Read a complex number (Complex) public final synchronized Complex readComplex()
Read a phasor (Phasor) public final synchronized Phasor readPhasor()
Read true or false (boolean) public final synchronized boolean readBoolean()
End of line status public boolean eol()
End of file status public boolean eof()
File existence status public boolean fileFound()
Number of lines in the file public final synchronized int numberOfLines()
Copy the file public final synchronized void copy(String copyFilename)
Close the file public final synchronized void close()
Get the file name public String getFileName()
Get the file name minus its extension public String getStemName()
Get the path name public String getPathName()
Get the directory path public String getDirPath()

METHODS

CONSTRUCTOR
public FileInput(String filename)
Usage:                      FileInput fin = new FileInput(filename);
Creates a new instance of FileInput that opens an existing text file called filename. The argument may be simply the file name, e.g. ""inputData.txt, if opening a file in the working directory. If the file is in another directorry, filename must be the full path name, e.g. on a MicroSoft operating system; "C:\\JavaProg\\Experiments\\Exp1\\inputData2.txt". The file name should contain the relevant extension, e.g. filename = "xydata.txt". See FileChooser for choosing the filename through an Open File dialog window.
Remember you must also close the file opened once you have finished reading from it (see close() method).

ENTERING A DOUBLE FLOATING POINT NUMBER (double)
public final synchronized float readFloat()
Usage:                      double x = fin.readDouble();
A method for reading a floating point number, of type double, from the the text file accessed by fin. The double number to be read must be separated from any preceding and/or any following number or word by a single space or several spaces, a tab, a comma, a semicolon, colon or end of line.

ENTERING A FLOAT FLOATING POINT NUMBER (float)
public final synchronized float readFloat()
Usage:                      float x = fin.readFloat();
A method for reading a floating point number, of type float, from the the text file accessed by fin. The float number to be read must be separated from any preceding and/or any following number or word by a single space or several spaces, a tab, a comma, a semicolon, colon or end of line.

ENTERING A BIGDECIMAL NUMBER (BigDecimal)
public final synchronized BigDecimal readBigDecimal()
Usage:                      BigDecimal x = fin.readBigDecimal();
A method for reading a BigDecimal, from the the text file accessed by fin. Enter the number using the standard convention, e.g. 1.23456, -4.3257, 4.3976e7, 9.52398e-5, 3456, -123456789. The BigDecimal number to be read must be separated from any preceding and/or any following number or word by a single space or several spaces, a tab, a comma, a semicolon, colon or end of line.

ENTERING AN INTEGER (int)
public final synchronized int readInt()
Usage:                      int x = fin.readInt();
A method for reading an integer number, of type int, from the the text file accessed by fin. The int number to be read must be separated from any preceding and/or any following number or word by a single space or several spaces, a tab, a comma, a semicolon, colon or end of line.

ENTERING A LONG INTEGER (long)
public final synchronized long readLong()
Usage:                      long x = fin.readLong();
A method for reading an integer number, of type long, from the the text file accessed by fin. The long number to be read must be separated from any preceding and/or any following number or word by a single space or several spaces, a tab, a comma, a semicolon, colon or end of line.

ENTERING A BIG INTEGER (BigInteger)
public final synchronized BigInteger readBigInteger()
Usage:                      BigInteger x = fin.readBigInteger();
A method for reading an integer, of type BigInteger, from the the text file accessed by fin. Enter the number using the standard convention, e.g. 3456, -123456789. The BigInteger number to be read must be separated from any preceding and/or any following number or word by a single space or several spaces, a tab, a comma, a semicolon, colon or end of line.

ENTERING A SHORT INTEGER (short)
public final synchronized short readShort()
Usage:                      short x = fin.readShort();
A method for reading an integer number, of type short, from the the text file accessed by fin. The short number to be read must be separated from any preceding and/or any following number or word by a single space or several spaces, a tab, a comma, a semicolon, colon or end of line.

ENTERING A BYTE INTEGER (byte)
public final synchronized byte readByte()
Usage:                      byte x = fin.readByte();
A method for reading an integer number, of type byte, from the the text file accessed by fin. The byte number to be read must be separated from any preceding and/or any following number or word by a single space or several spaces, a tab, a comma, a semicolon, colon or end of line.

ENTERING A SEPARATED CHARACTER (char)
public final synchronized char readChar()
Usage:                      char x = fin.readChar();
A method for reading a single character (type char), separated from adjacent characters, from the the text file accessed by fin. The character must be separated from any preceding and/or any following number or word by a single space or several spaces, a tab, a comma, a semicolon, colon or end of line.

See FileInputAsChar for a separate class containing methods enabling a file to be read in as a stream of char, Char or the ISO code int equivalents.

ENTERING TRUE OR FALSE (boolean)
public final synchronized boolean readBoolean()
Usage:                      lboolean x = fin.readBoolean();
A method for reading true or false, of type boolean, from the the text file accessed by fin. The boolean to be read must be separated from any preceding and/or any following number or word by a single space or several spaces, a comma or a tab.

ENTERING A WORD OF TEXT
public final synchronized String readWord()
Usage:                      String x = fin.readWord();
A method for reading a word of text from the the text file accessed by fin. This method will read succcessive characters from the text file until single space or several spaces, a tab, a comma, a semicolon, colon or end of line is met. The characters are returned to a string.
See immediately below for reading a word separated only by spaces.

ENTERING A WORD OF TEXT SEPARATED ONLY BY SPACES
public final synchronized String readWordSpaceOnly()
Usage:                      String x = fin.readWordSpaceOnly();
A method for reading a word of text from the the text file accessed by fin. This method will read succcessive characters from the text file until a space is met. The characters are returned to a string.
See immediately above for reading a word separated by any of the separators; single space, several spaces, a tab, a comma, a semicolon or a colon.

ENTERING A LINE OF TEXT
public final synchronized String readLine()
Usage:                      String x = fin.readLine();
A method for reading a line of text from the the text file accessed by fin. This method will read succcessive characters from the text file until a line return character is met. The characters are returned to a string.

ENTERING A COMPLEX NUMBER (Complex)
public final synchronized Complex readComplex()
Usage:                      Complex x = fin.readComplex();
A method for reading a complex number from the the text file accessed by fin. The user may enter a complex number in one of the following formats:
x+jy    x+iy    x-jy    x-iy
    e.g. 2.3e-5-j0.453
NO spaces are allowed within the complex number.

ENTERING A PHASOR (Phasor)
public final synchronized Phasor readPhasor()
Usage:                      Phasor x = fin.readPhasor();
A method for reading a Phasor from the the text file accessed by fin. The user may enter a Phasor in one of the following formats:
x<y    xLy    x<ydeg    xLydeg    x<yrad    xLyrad        e.g. 3.0e2<22.78deg
where x is the magnitude and y is the phase. The first four formats accept the phase in degrees, the latter two formats accept the phase in radians. The default value may be entered as a Phasor variable or as a String using one of the above formats.

OBTAINING THE NUMBER OF LINES IN THE FILE
public final synchronized int numberOfLines()
Usage:                      int nLines = fin.numberOfLines();
This method returns the number of lines in the the text file accessed by fin.

MAKING A COPY OF THE FILE
public final synchronized void copy(String copyFilename)
Usage:                      fin.copy(copyFilename);
This method returns a copy of the file to the working directory. The copy file name is supplied via the method argument, copyFilename.

END OF LINE STATUS
public boolean eol()
Usage:                      boolean eol = fin.eol();
Returns true if the last item read was an end of line item, returns false if the last item read was not an end of line item.

END OF FILE STATUS
public boolean eof()
Usage:                      boolean eof = fin.eof();
Returns false if no attempt to read past the emd of file is made. Returns true if an attempt is made to read past the end of the file. Attempts to read a double, float, int or long after the end of file leads to zero being returned. An attempt to read a char after the end of file leads to a space being returned. An attempt to read a String after the end of file leads to an empty string being returned. In each case an error message is written to the screen.

FILE EXISTENCE STATUS
public boolean fileFound()
Usage:                      boolean ffd = fin.fileFound();
Returns true if the file whose filename has been passed to the instance of FileInput, fin, was found. Returns false if that file was not found.

GETTING THE PATH
public String getPathName()
Usage:                      String path = fin.getPathName();
A method for returning the path name, e.g. "C:\JavaProg\Experiments\Exp1\inputData2.txt".

GETTING THE FILE NAME
public String getFileName()
Usage:                      String name = fin.getFileName();
A method for returning the file name, e.g. "inputData2.txt".

GETTING THE FILE NAME WITHOUT ITS EXTENSION
public String getStemName()
Usage:                      String name = fin.getStemName();
A method for returning the file name without its extension, e.g. "inputData2".

GETTING THE PATH TO THE DIRECTORY HOLDING THE INPUT FILE
public String getDirPath()
Usage:                      String dir = fin.getDirPath();
A method for returning the path to the directory holding bthe input file, e.g. "C:\JavaProg\Experiments\Exp1" where the full path name is "C:\JavaProg\Experiments\Exp1\inputData2.txt".

CLOSING THE FILE
public final synchronized void close()
Usage:                      fin.close();
A method for closing the text file opened by fin.



OTHER CLASSES USED BY THIS CLASS

This class uses the following classes in this library:



This page was prepared by Dr Michael Thomas Flanagan