Michael Thomas Flanagan's Java Scientific Library

FileChooser Class:      Selecting a File and Reading from that File

     

Last update: 31 October 2010                                                                                                                              Main Page of Michael Thomas Flanagan's Java Scientific Library

This class contains methods for opening a dialog window through which a SINGLE FILE can be selected for opening and for reading numbers and characters, of the following types, from the opened text file:
  • floating point, double;
  • floating point, float;
  • arbitrary precision, BigDecimal;
  • complex number, Complex;
  • phasor, Phasor;
  • boolean, boolean;
  • isolated character, char.
        
  • integer, int;
  • long integer, long;
  • big integer, BigInteger;
  • short integer, short;
  • byte integer, byte;
  • word, a String terminated by a space;
  • line, a String terminated by a new line return;
and methods for returning the number of lines in the file and the file name and path.

See the class MultipleFilesChooser for selecting and reading from several files.

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

SUMMARY OF CONSTRUCTOR AND METHODS

Constructors public FileChooser()
public FileChooser(String path)
File extension filters public void setExtension(String extension)
public void setAllExtensions()
public String getExtension()
Directory path public void setPath(String path)
public String getPath()
Open an Open File dialog window and select a file public String selectFile()
public String selectFile(String windowTitle)
End the program public static final synchronized void.endProgram()
System.exit(0)
METHODS INHERITED FROM THE SUPERCLASS, FileInput
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()
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 character (char) public final synchronized char readChar()
Read a word of text (String) public final synchronized String readWord()
Read a line of text (String) public final synchronized String readLine()
Read a complex number (Complex) public final synchronized Complex readComplex()
Read a phasor number (Phasor) public final synchronized Phasor readPhasor()
Read true or false (boolean) public final synchronized boolean readBoolean()
Number of lines in the file public final synchronized int numberOfLines()
End of line status public String eol()
End of file status public String eof()
File existence status public boolean fileFound()
Close the file public final synchronized void close()

CONSTRUCTORS AND METHODS

CONSTRUCTORS
public FileChooser()
public FileChooser(String path)
Usage:                      FileChooser fc = new FileChooser();
Creates a new instance of FileChooser. When selectFile is called the directory displayed in the Open File dialog window will be the current directory. The window allows all non-hidden directories to be browsed. As selectFile opens a window any program calling selectFile must be terminated by the endProgram() method or a System.exit(0) command.
Remember you must also close the file opened once you have finished reading from it (see close() method).

Usage:                      FileChooser fc = new FileChooser(path);
Creates a new instance of FileChooser. When selectFile is called the directory displayed in the Open File dialog window will be that defined by the argument, path. The format of the path String must conform to the convention of the operating system, e.g. if a Microsoft Systems is being used, an example of path would be "C:\\Java_Programming\\flanagan_library\\io_files". The window allows all non-hidden directories to be browsed. As selectFile opens a window any program calling selectFile must be terminated by the endProgram() method or a System.exit(0) command.
Remember you must also close the file opened once you have finished reading from it (see close() method).



FILE NAME EXTENSION FILTERS
Set a file name extension filter
public void setExtension(String extension)
Usage:                      fc.setExtension(extension);
If this method is called before selectFile is called the Open File dialog window will display only folders and files with the same extenstion as that passed as the String argument, extension. The format of this String should be the extension, e.g, "txt", "doc", "xls", "ddt", ... If setExtension is not called, the Open File dialog window displays all folders and all files.

Remove the file name extension filter
public void setAllExtensions()
Usage:                      fc.setAllExtensions();
This method removes any file extension filter (see immediately above). After this method is called the Open File dialog window will display all folders and all files.

Get the current file name extension filter
public String getExtension()
Usage:                      extension = fc.getExtension();
This method returns the extension applied by a current extension filter, e.g. "txt". It returns null if no extension filter is currently operating.



DIRECTORY PATH
Reset the path to the initially displayed directory
public void setPath(String path)
Usage:                      fc.setPath(path);
This method allows the path to the directory displayed, on opening, by selectFile to be reset. The format of the path String must conform to the convention of the operating system, e.g. if a Microsoft Systems is being used, an example of path would be "C:\\Java_Programming\\flanagan_library\\io_files".

See inherited method, getDirPath() (below), for method returning the path to the directory selected.

DISPLAY AN OPEN FILE DIALOG WINDOW AND CHOOSE A FILE
public String selectFile()
public String selectFile(String windowTitle)
Usage:                      filename = fc.selectFile();
Displays an Open File dialog window. If no path argument was passed to the constructor the directory displayed in the Open File dialog window will be the current directory. The window allows all non-hidden directories to be browsed. On selecting a file its file name is returned as a string. As selectFile opens a window any program calling selectFile must be terminated by the endProgram() method or a System.exit(0) command
Remember you must also close the file opened once you have finished reading from it (see close() method).

Usage:                      filename = fc.selectFile(windowTitle);
Displays an Open File dialog window. If no path argument was passed to the constructor the directory displayed in the Open File dialog window will be the current directory. The window allows all non-hidden directories to be browsed. The argument windowTitle is displayed as the Open File dialog window title. This may be used as a prompt message line, e.g. "Open first data input file". The default title is "Select a file". On selecting a file its file name is returned as a string. As selectFile opens a window any program calling selectFile must be terminated by the endProgram() method or a System.exit(0) command.
Remember you must also close the file opened once you have finished reading from it (see close() method).

ENDING AN APPLICATION USING METHODS FROM CLASS FileChooser
public static final synchronized void EndProgram()
Usage:
                     fc.endProgram();

This method displays a dialogue box that asks if you want to end the program. Answering yes ends the program. Answering no displays a message telling you how you may now end the program.

Any application that you write that displays a graphical user interface, as the methods in this class Db do, must be terminated with the line
System.exit(0)
or by calling a method that contains that statement, e.g. by calling endProgram().



METHODS INHERITED FROM SUPERCLASS FileInput
The following methods are all inherited from FileInput

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

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

GETTING THE SELECTED FILE NAME MINUS ITS EXTENSION
public String getStemName()
Usage:                      String name = fc.getStemName();
A method for returning the file name without the 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:                      fc.close();
A method for closing the text file opened by fc.



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

ENTERING A BIGDECIMAL NUMBER (BigDecimal)
public final synchronized BigDecimal readBigDecimal()
Usage:                      BigDecimal x = fc.readBigDecimal();
A method for reading a BigDecimal from the the text file accessed by fc. 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 = fc.readInt();
A method for reading an integer number, of type int, from the the text file accessed by fc. The int number to be read may be separated from any preceding and/or any following number or word by a single space or several spaces, a comma, tab, a comma, a semicolon, colon or end of line.

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

ENTERING A BIG INTEGER (BigInteger)
public final synchronized BigInteger readBigInteger()
Usage:                      BigInteger x = fc.readBigInteger();
A method for reading an integer, of type BigInteger, from the the text file accessed by fc. 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 = fc.readShort();
A method for reading an integer number, of type short, from the the text file accessed by fc. 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 = fc.readByte();
A method for reading an integer number, of type byte, from the the text file accessed by fc. 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 CHARACTER (char)
public final synchronized char readChar()
Usage:                      char x = fc.readChar();
A method for reading a single character (type char) from the the text file accessed by fc. The character must be preceded by a space, tab or comma and be followed by a space, tab, comma or end of line. To read individual characters in a line of text use the method readLine() and access the individual characters in the resulting String in your own program.

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

ENTERING A WORD OF TEXT
public final synchronized String readWord()
Usage:                      String x = fc.readWord();
A method for reading a word of text from the the text file accessed by fc. This method will read succcessive characters from the text file until a space, a tab or a line return character is met. The characters are returned to a string.

ENTERING A LINE OF TEXT
public final synchronized String readLine()
Usage:                      String x = fc.readLine();
A method for reading a line of text from the the text file accessed by fc. 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 = fc.readComplex();
A method for reading a complex number from the the text file accessed by fc. The user may enter a complex number in one of the following formats:
x+jy    x+iy    x + jy    x + iy    x-jy    x-iy    x - jy    x - iy            e.g. 2.3e-5 - j0.453

ENTERING A PHASOR (Phasor)
public final synchronized Phasor readPhasor()
Usage:                      Phasor x = fc.readPhasor();
A method for reading a Phasor from the the text file accessed by fc. 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.

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 = fc.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 = fc.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.



EXAMPLE PROGRAM

The example program Plotter.java illustrates the use of FileChooser in reading data, to be plotted using PlotGraph, from a text file.
A data file for use with this example can be found on TestPlotter.txt



OTHER CLASSES USED BY THIS CLASS

This class uses the following classes in this library:



This page was prepared by Dr Michael Thomas Flanagan