Server Side JavaScript and File Manipulation
by Thomas Valentine
|
This weeks JavaScript tutorial will show you the most common
methods for file manipulation in JavaScript.
|
This short tutorial will show you the most common methods
for file manipulation in JavaScript
When designing your server side JavaScript script for
your site, you may realize the need to access and manipulate
not only your databases, but simple files as well. Server
side JavaScript has recognized this and created the "File"
object. With it you may read and write to simple files, the
most common being ASCII text files (.txt extension). There
are eighteen methods which pretty much covers all of the
functionality you'll need in working with your files on the
server from the web site pages. They are as follows:
- byteToString() - This method converts the byte numbers
passed as the parameter to the method into its textual,
string equivalent.
- clearError() - This method is used to clear the "file
error" status on a file stated within the file.eof and
file.error log files.
- close() - This method is used to simply close the file
which you opened and possibly worked with earlier.
- eof() - This method is used to tell you if you
are at the end of the file you have opened and are using.
"EOF" means End Of File.
- error() - This method returns the current error,
if any.
- exists() - This method is used to tell you if the
file you wish to work with actually exists.
- flush() - This method will write the contents of
the buffer to the specified file, which is the file you've
just opened. It is similar to the copy and paste from the
clipboard action within Windows computers.
- getLength() - This method is used to tell you the
length of the file, in bytes.
- getPosition() - This method is used to tell you
the current position within the file you are working
with.
- open() - This method is used to open the desired
file and ready it for your changes.
- read() - This method reads the number of
characters within a string.
- readByte() - This method reads the next character
(byte) which is next within the file.
- readln() - This method reads the line of text
(bytes) which follows the current position within the
file.
- setPosition() - This method sets the position you
will be working at within the file.
- stringToByte() - This method is used to convert
the string passed as the parameter to the method into its
byte code equivalent.
- write() - This method is used to write your text
to the file which was opened, and which you are working
with.
- writeByte() - This method is used to write binary
data to a binary file which you have opened.
- writeln() - This method is used to write the text you
desire to the file, and includes a carriage return character
at the end of the text.
You can see the huge amount of functionality available to
you for working with files on the server. While you'll use
all of the above methods eventually, it is the open() method
which you will use the most. This makes sense because you'll
have to use it to open every file you work with. Because of
this high amount of use, there are some properties of the
open() method which I feel should be mentioned here. They're
fairly simple and straightforward, and are as follows.
- a - This is used to open a file and ready it to
be appended to. If the file doesn't exist, it is created.
Only text files will be created, of any extension. A value
of true is always returned.
- a+ - This is used to open a file and ready it to
be read and appended to. If the file doesn't exist, it is
created. Only text files will be created, of any extension.
A value of true is always returned.
- r - This is used to open a file and ready it to
be read. If the file doesn't exist, a value of false is
returned. If it does exist, a value of true is
returned.
- r+ - This is used to open a file and ready it to
be read and written to. If the file doesn't exist, a value
of false is returned. If it does exist, a value of true is
returned. It should be noted that both the reading and
writing of the file will begin at the beginning of the
file.
- w - This is used to open a file and ready it to
be written to. If the file doesn't exist, it is created. If
the file already exists, it is overwritten. Only text files
will be created, of any extension. A value of true is always
returned.
- w+ - This is used to open a file and ready it to
be read and written to. If the file doesn't exist, it is
created. If the file already exists, it is overwritten. Only
text files will be created, of any extension. A value of
true is always returned.
- optionb - Using the b option with any of the
above options specifies that a binary file is to be read,
written, appended or created, according to the option
given.
Thanks for joining us here at the JavaScript Chronicles.
Swing by next week when we cover JavaScript and forms.
Server Side JavaScript Mail Sending
The JavaScript Chronicles
Working with Forms in JavaScript
The JavaScript Chronicles
JavaScript Introduction
Part 2: Data Types
Part 3: Arrays
Part 4: Operators
Part 5: Conditional Statements
Part 6: JavaScript Functions
Part 7: Pattern Matching - The RegExp Object
Part 8: Introduction to Server Side JavaScript
Part 9: Server Side JavaScript Mail Sending
Part 10: Server Side JavaScript and File Manipulation
Part 11: Working with Forms in JavaScript
Part 12: Getting to Know Dynamic HTML
|