Michael Thomas Flanagan's Java Scientific Library

MultipleFilesChooser Class:      Selecting Several Files and Reading from those Files

     

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 SEVERAL files can be selected for opening and for reading numbers and characters, of the following types, from the opened text files:
  • 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 files and the files' names and paths.

See FileChooser for a more convenient class if ONLY ONE FILE is to be selected

import directive: import flanagan.io.*;

Related classes:

SUMMARY OF CONSTRUCTOR AND METHODS

Constructors public MultipleFilesChooser()
public MultipleFilesChooser(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 the files public FileInput[] selectFiles()
public FileInput[] selectFiles(String windowTitle)
Get the file names public String[] getFileNames()
Get the file names minus their extensions public String[] getStemNames()
Get the path names public String[] getPathNames()
Get the directory paths public String[] getDirPaths()
Get the number of files selected public int[] getNumberOfFiles()
Close an individual file or close all files public final synchronized void close()
End the program public static final synchronized void.endProgram()
System.exit(0)
Methods from the associated class FileInput
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 (Phasor) public final synchronized Phasor readPhasor()
Read true or false (boolean) public final synchronized boolean readBoolean()
Number of lines in the files public final synchronized int numberOfLines()
End of lile status public String eol()
End of file status public String eof()
File existence status public boolean fileFound()
Close an individual file or close all files public final synchronized void close()

CONSTRUCTORS AND METHODS

CONSTRUCTORS
public MultipleFilesChooser()
public MultipleFilesChooser(String path)
Usage:                      MultipleFilesChooser mfc = new MultipleFilesChooser();
Creates a new instance of MultipleFilesChooser. When selectFiles 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 selectFiles opens a window any program calling selectFiles must be terminated by the endProgram() method or a System.exit(0) command.
Remember you must also close the files opened once you have finished reading from them (see close() method).

Usage:                      MultipleFilesChooser mfc = new MultipleFilesChooser(path);
Creates a new instance of MultipleFilesChooser. When selectFiles 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 selectFiles opens a window any program calling selectFiles must be terminated by the endProgram() method or a System.exit(0) command.
Remember you must also close the files opened once you have finished reading from them (see close() method).


FILE NAME EXTENSION FILTERS
Set a file name extension filter
public void setExtension(String extension)
Usage:                      mfc.setExtension(extension);
If this method is called before selectFiles 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:                      mfc.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 = mfc.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:                      mfc.setPath(path);
This method allows the path to the directory displayed, on opening, by selectFiles 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, getDirPaths() (below), for method returning the path to the directory selected.

DISPLAY AN OPEN FILE DIALOG WINDOW AND SELECT FILES
public FileInput[] selectFiles()
public FileInput[] selectFiles(String windowTitle)
Usage:                      fileReference = mfc.selectFiles();
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. To select more than one file press the shift key when clicking on the file names. This method returns an array of FileInput references; each reference is to the corresponding instance of FileInput created for that selected file. The references are in the same order as the array of selected filenames which can be accessed through the method getFileNames. These references may then be used to access any of the methods in the class FileInput, e.g.
                     double xx = fileReference[2].readDouble();
The main methods are listed below
As selectFiles opens a window any program calling selectFiles 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 = mfc.selectFiles(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". To select more than one file press the shift key when clicking on the file names. This method returns an array of FileInput references; each reference is to the corresponding instance of FileInput created for that selected file. The references are in the same order as the array of selected filenames which can be accessed through the method getFileNames. These references may then be used to access any of the methods in the class FileInput, e.g.
                     double xx = fileReference[2].readDouble();
The main methods are listed below
As selectFiles opens a window any program calling selectFiles 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).



GETTING THE SELECTED FILE NAMES
public String[] getFileNames()
Usage:                      String[] names = mfc.getFileNames();
A method for returning the selected file names, e.g. "inputData1.txt", "inputData1.txt", etc.

GETTING THE SELECTED FILE NAMES MINUS THE EXTENSIONS
public String[] getStemNames()
Usage:                      String[] snames = mfc.getStemNames();
A method for returning the selected file names without their extensions, e.g. "inputData1", "inputData1", etc.

GETTING THE SELECTED PATHS
public String[] getFilePath()
Usage:                      String[] paths = mfc.getPathNames();
A method for returning the path names, e.g. "C:\JavaProg\Experiments\Exp1\inputData1.txt", "C:\JavaProg\Experiments\Exp1\inputData2.txt",etc

GETTING THE PATHS TO THE DIRECTORY HOLDING THE SELECTED FILES
public String getDirPaths()
Usage:                      String dir = mfc.getDirPaths();
A method for returning the paths to the directory holding the input files, e.g. "C:\JavaProg\Experiments\Exp1" where the full path name is "C:\JavaProg\Experiments\Exp1\inputData2.txt".

ENDING AN APPLICATION USING METHODS FROM CLASS MultipleFilesChooser
public static final synchronized void EndProgram()
Usage:
                     mfc.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 IN THE CLASS FileInput
The following methods are all from the class FileInput
The method, described above, selectFiles, returns an array of instances of FileInput for all the selected files. Each of those instances can then be treated in exactly the same way as one would if you had created ann instance of FileInput directly.

ENTERING A DOUBLE FLOATING POINT NUMBER (double)
public final synchronized float readFloat()
Usage:                      double x = fileReference[i].readDouble();
A method for reading a floating point number, of type double, from the the text file accessed by fileReference[i]. 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 or a tab.

ENTERING A FLOAT FLOATING POINT NUMBER (float)
public final synchronized float readFloat()
Usage:                      float x = fileReference[i].readFloat();
A method for reading a floating point number, of type float, from the the text file accessed by fileReference[i]. 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 or a tab.

ENTERING A BIGDECIMAL NUMBER (BigDecimal)
public final synchronized BigDecimal readBigDecimal()
Usage:                      BigDecimal x = fileReference[i].readBigDecimal();
A method for reading a BigDecimal from the the text file accessed by fileReference[i]. 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 = fileReference[i].readInt();
A method for reading an integer number, of type int, from the the text file accessed by fileReference[i]. 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 or a tab.

ENTERING A LONG INTEGER (long)
public final synchronized long readLong()
Usage:                      long x = fileReference[i].readLong();
A method for reading an integer number, of type long, from the the text file accessed by fileReference[i]. 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.

ENTERING A BIG INTEGER (BigInteger)
public final synchronized BigInteger readBigInteger()
Usage:                      BigInteger x = fileReference[i].readBigInteger();
A method for reading an integer, of type BigInteger, from the the text file accessed by fileReference[i]. Enter the number using the standard convention, e.g. 34564367, -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 = fileReference[i].readShort();
A method for reading an integer number, of type short, from the the text file accessed by fileReference[i]. 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 = fileReference[i].readByte();
A method for reading an integer number, of type byte, from the the text file accessed by fileReference[i]. 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 = fileReference[i].readChar();
A method for reading a single character (type char) from the the text file accessed by fileReference[i]. 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 = fileReference[i].readBoolean();
A method for reading true or false, of type boolean, from the the text file accessed by fileReference[i]. 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 or a tab.

ENTERING A WORD OF TEXT
public final synchronized String readWord()
Usage:                      String x = fileReference[i].readWord();
A method for reading a word of text from the the text file accessed by fileReference[i]. 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 = fileReference[i].readLine();
A method for reading a line of text from the the text file accessed by fileReference[i]. 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 = fileReference[i].readComplex();
A method for reading a complex number from the the text file accessed by fileReference[i]. 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 = fileReference[i].readPhasor();
A method for reading a Phasor from the the text file accessed by fileReference[i]. 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 FILES
public final synchronized int numberOfLines()
Usage:                      int nLines = fileReference[i].numberOfLines();
This method returns the number of lines in the the text file accessed by fileReference[i].



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 = fileReference[i].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 = fileReference[i].fileFound();
Returns true if the file whose filename has been passed to the instance of FileInput, fileReference[i], was found. Returns false if that file was not found.

CLOSING AN INDIVIDUAL FILE OR ALL THE FILES
public final synchronized void close()
Closing all the selected files in one call
To close all the files that have been selected in one call attach the method close() to the MultipleFilesChooser instance:
Usage:                      mfc.close();
Closing an individual selected file
To close an individual file attach the method close() to the relevant instance that was returned by the selectFiles method:
Usage:                      fileReference[i].close();



OTHER CLASSES USED BY THIS CLASS

This class uses the following classes in this library:



This page was prepared by Dr Michael Thomas Flanagan