FileReader2 Applet
FileReader is a helper applet designed for use by JavaScript programmers. FileReader allows you
to read data from an ASCII file on your web server. Binary data is not supported. Click one or both
of the links below to see the applet in action. Each link calls a JavaScript function to read the data
from a file on my server called "testFile.txt". The text in the file is displayed in a confirmation
dialog box.
confirmData('testFile.txt')
getLines('testFile.txt')
Here is the html used to put the applet on this page:
Here is a link to the FileReader.class file. (Shift + Click)
Here is a link to the testFile.txt file. (Shift + Click)
Here is a link to the new FileReader2.class file. (Shift + Click)
(FileReader2 is identical to FileReader except that it uses a newer Java API and is compatible with
the Netscape 6.2 browser.)
Here is the JavaScript code for the two functions used in this very simple example:
The applet provides the following public interface:
public int readFile(String strFileName);
public int getNumLines();
public String getFileName();
public String getLine(int nWhichLine);
public String getLines();
New --public double getLineByValue(int nWhichLine);
New --public double getLineByValue(String strWhichLine);
Each call to public double getLineByValue() returns the value in a given line of text, interpreted as a
double-precision floating point value. The applet interprets the data in that given line UP TO the first
blank space character. Everything after the first space character is ingored. Example: The line "100.25 //this is a comment"
will be interpreted as 100.25 and will not generate an error message. This enables commenting.
Each call to public int readFile(String strFileName) re-initializes the applet's internal memory structure and
reads the data from the file named strFileName, which must exist in the same folder containing the
FileReader.class file. The return value is normally the number of lines of text read from the file. However,
a negative value signifies an error: -1 = MalformedURLExcepton and -2 = IOException. Only one file at a time is allowed to be placed into the applet's internal
memory structure, but you may have as many instances of the applet running concurrently as you like.
Just give each instance a different name in the "name =" portion of the applet html code.
To find out how many lines of text are contained in the current file, use a call to public
int getNumLines(). If you haven't yet called readFile() to open the file and read the contents,
this will return a value of 0.
You might want to find out if you have already opened and read the contents of a particular file in
order to avoid unnecessarily re-opening and re-reading the same file. Or, you might simply need to
know the name of the current file. Use a call to public String getFileName() to find this out.
This method will return the name of the current file or "null" if no file has yet been opened and
read.
Once you have opened and read the contents of a file into the applet's internal memory structure,
you can access this information with a call to public String getLine(int nWhichLine) or with
a call to public String getLines(). Use getLine() when you are only interested in the contents
of a particular line of text or when you want to individually parse the text contained in the file on
a line-by-line basis. Use getLines() if you just want to read all the text at once. Each of these
methods will return a value of "null" in the event of an error, such as trying to get the 50th line
in a file only containing 30 lines or trying to get the text from a file and you haven't yet opened and
read a file into the applet's internal memory structure.
Click javascript:getLines('NoSuchFile.txt') to find out
what happens when you try to read a non-existent file. Although I haven't done so in these
simple examples, you might want to test the return values against "null" or, in the case of readFile(), against
return values of less-than-zero to ensure that you are getting
valid data before attempting to format the data in a specific way.
The applet only supports 2 applet parameters: Author and Origin, which should be set to "Mark Ganson"
and "http://www.bigfoot.com/~java4free", respectively. If these parameters are not set, a message will be
displayed in the browser's status bar indicating the origin of the applet.
As always, you're questions/suggestions/comments/flames are always welcome, so e-mail me
Java4Free! home