Table of Contents | Previous
| Next
| Index
Lets an application interact with a physical file on the server.
The File
constructor:
new File(path)
You can use the File
object to write to or read from a file on the server. For security reasons, you cannot programmatically access the file system of client machines.
You can use the File
object to generate persistent HTML or data files without using a database server. Information stored in a file is preserved when the server goes down.
Exercise caution when using the File
object. An application can read and write files anywhere the operating system allows. If you create an application that writes to or reads from your file system, you should ensure that users cannot misuse this capability.
Specify the full path, including the filename, for the path
parameter of the File
object you want to create. The path must be an absolute path; do not use a relative path.
If the physical file specified in the path already exists, the JavaScript runtime engine references it when you call methods for the object. If the physical file does not exist, you can create it by calling the open
method.
You can display the name and path of a physical file by calling the write
function and passing it the name of the related File
object.
A pointer indicates the current position in a file. If you open a file in the a
or a+
mode, the pointer is initially positioned at the end of the file; otherwise, it is initially positioned at the beginning of the file. In an empty file, the beginning and end of the file are the same. Use the eof
, getPosition
, and setPosition
methods to specify and evaluate the position of the pointer. See the open
method for a description of the modes in which you can open a file.
You can use the prototype
property of the File
object to add a property to all File
instances. If you do so, that addition applies to all File
objects running in all applications on your server, not just in the single application that made the change. This allows you to expand the capabilities of this object for your entire server.
In addition, this object inherits the watch
and unwatch
methods from Object
.
Example 1. The following example creates the File
object userInfo
that refers to a physical file called info.txt
. The info.txt
file resides in the same directory as the application's .web
file:
userInfo = new File("info.txt")
Example 2. In the following example, the File
object refers to a physical file with an absolute path:
userInfo = new File("c:\\data\\info.txt")
Example 3. The following example displays the name of a File
object onscreen.
userInfo = new File("c:\\data\\info.txt")
write(userInfo)
Converts a number that represents a byte into a string.
byteToString(number)
Use the stringToByte
and byteToString
methods to convert data between binary and ASCII formats. The byteToString
method converts the number
argument into a string.
Because byteToString
is a static method of File
, you always use it as File.byteToString()
, rather than as a method of a File
object you created.
If the argument you pass into the byteToString
method is not a number, the method returns an empty string.
The following example creates a copy of a text file, one character at a time. In this example, a while
loop executes until the pointer is positioned past the end of the file. Inside the loop, the readByte
method reads the current character from the source file, and the byteToString
method converts it into a string; the write
method writes it to the target file. The last readByte
method positions the pointer past the end of the file, ending the while
loop. See the File
object for a description of the pointer.
// Create the source File object
source = new File("c:\data\source.txt")
// If the source file opens successfully, create a target file
if (source.open("r")) {
target = new File("c:\data\target.txt")
target.open("w")
// Copy the source file to the target
while (!source.eof()) {
data = File.byteToString(source.readByte())
target.write(data);
}
source.close()
}
target.close()
This example is similar to the example used for the write
method of File
. However, this example reads bytes from the source file and converts them to strings, instead of reading strings from the source file.
File.stringToByte
Clears the current file error status.
clearError()
None.
The clearError
method clears both the file error status (the value returned by the error
method) and the value returned by the eof
method.
See the example for the error
method.
File.error
, File.eof
Closes an open file on the server.
close()
None.
When your application is finished with a file, you should close the file by calling the close
method. If the file is not open, the close
method fails. This method returns true if it is successful; otherwise, it returns false.
See the examples for the open
method.
File.open
, blob
Specifies the function that creates an object's prototype. Note that the value of this property is a reference to the function itself, not a string containing the function's name.
See Object.constructor
.
Determines whether the pointer is beyond the end of an open file.
eof()
None.
Use the eof
method to determine whether the position of the pointer is beyond the end of a file. See File
for a description of the pointer.
A call to setPosition
resulting in a location greater than fileObjectName.getLength
places the pointer beyond the end of the file. Because all read operations also move the pointer, a read operation that reads the last byte of data (or character) in a file positions the pointer beyond the end of the file.
The eof
method returns true if the pointer is beyond the end of the file; otherwise, it returns false.
In this example, a while
loop executes until the pointer is positioned past the end of the file. While the pointer is not positioned past the end of the file, the readln
method reads the current line, and the write
method displays it. The last readln
method positions the pointer past the end of the file, ending the while
loop.
x = new File("c:\data\userInfo.txt")
if (x.open("r")) {
while (!x.eof()) {
line = x.readln()
write(line+"<br>");
}
x.close();
}
File.getPosition
, File.setPosition
Returns the current error status.
error()
None
0 if there is no error.
-1 if the file specified in fileObjectName
is not open
Otherwise, the method returns a nonzero integer indicating the error status. Specific error status codes are platform-dependent. Refer to your operating system documentation for more information.
The following example uses the error
method in an if
statement to take different actions depending on whether a call to the open
method succeeded. After the if
statement completes, the error status is reset with the clearError
method.
userInput = new File("c:\data\input.txt")
userInput.open("w")
if (userInput.error() == 0) {
fileIsOpen() }
else {
fileIsNotOpen() }
userInput.clearError()
File.clearError
Tests whether a file exists.
exists()
None.
True if the file exists; otherwise, false.
The following example uses an if
statement to take different actions depending on whether a physical file exists. If the file exists, the JavaScript runtime engine opens it and calls the writeData
function. If the file does not exist, the runtime engine calls the noFile
function.
dataFile = new File("c:\data\mytest.txt")
if (dataFile.exists() ==true) {
dataFile.open("w")
writeData()
dataFile.close()
}
else {
noFile()
}
Writes the content of the internal buffer to a file.
flush()
None.
When you write to a file with any of the File
object methods (write
, writeByte
, or writeln
), the data is buffered internally. The flush
method writes the buffer to the physical file. The flush
method returns true if it is successful; otherwise, it returns false.
Do not confuse the flush
method of the File
object with the top-level flush
function. The flush
function flushes a buffer of data and causes it to display in the client browser; the flush
method flushes a buffer of data to a physical file.
See the write
method for an example of the flush
method.
File.write
, File.writeByte
, File.writeln
Returns the length of a file.
getLength()
None.
If this method is successful, it returns the number of bytes in a binary file or characters in a text file; otherwise, it returns -1.
The following example copies a file one character at a time. This example uses getLength
as a counter in a for
loop to iterate over every character in the file.
// Create the source File object
source = new File("c:\data\source.txt")
// If the source file opens successfully, create a target file
if (source.open("r")) {
target = new File("c:\data\target.txt")
target.open("a")
// Copy the source file to the target
for (var x = 0; x < source.getLength(); x++) {
source.setPosition(x)
data = source.read(1)
target.write(data)
}
source.close()
}
target.close()
Returns the current position of the pointer in an open file.
getPosition()
None
-1 if there is an error.
Use the getPosition
method to determine the position of the pointer in a file. See the File
object for a description of the pointer. The getPosition
method returns the current pointer position; the first byte in a file is byte 0.
The following examples refer to the file info.txt
, which contains the string "Hello World." The length of info.txt
is 11 bytes.
Example 1. In the following example, the first call to getPosition
shows that the default pointer position is 0 in a file that is opened for reading. This example also shows that a call to the read
method repositions the pointer.
dataFile = new File("c:\data\info.txt")
dataFile.open("r")
write("The position is " + dataFile.getPosition() + "<BR>")
write("The next character is " + dataFile.read(1) + "<BR>")
write("The new position is " + dataFile.getPosition() + "<BR>")
dataFile.close()
This example displays the following information:
The position is 0
The next character is H
The new position is 1
Example 2. This example uses setPosition
to position the pointer one byte from the end of the eleven-byte file, resulting in a pointer position of offset 10.
dataFile = new File("c:\data\info.txt")
dataFile.open("r")
dataFile.setPosition(-1,2)
write("The position is " + dataFile.getPosition() + "<BR>")
write("The next character is " + dataFile.read(1) + "<BR>")
dataFile.close()
This example displays the following information:
The position is 10
The next character is d
Example 3. You can position the pointer beyond the end of the file and still evaluate getPosition
successfully. However, a call to eof
indicates that the pointer is beyond the end of the file.
dataFile.setPosition(1,2)
write("The position is " + dataFile.getPosition() + "<BR>")
write("The value of eof is " + dataFile.eof() + "<P>")
This example displays the following information:
The position is 12
The value of eof is true
File.eof
, File.open
, File.setPosition
Opens a file on the server.
open(mode)
Use the open
method to open a file on the server before you read from it or write to it. If the file is already open, the method fails and has no effect. The open
method returns true if it is successful; otherwise, it returns false.
The mode
parameter is a string that specifies whether to open the file to read, write, or append data. You can optionally use the b
parameter anytime you specify the mode. If you do so, the JavaScript runtime engine on the server opens the file as a binary file. If you do not use the b
parameter, the runtime engine opens the file as a text file. The b
parameter is available only on Windows platforms.
The possible values for mode
are as follows:
-
r[b]
opens a file for reading. If the file exists, the method succeeds and returns true; otherwise, the method fails and returns false.
-
w[b]
opens a file for writing. If the file does not already exist, it is created; otherwise, it is overwritten. This method always succeeds and returns true.
-
a[b]
opens a file for appending (writing at the end of the file). If the file does not already exist, it is created. This method always succeeds and returns true.
-
r+[b]
opens a file for reading and writing. If the file exists, the method succeeds and returns true; otherwise, the method fails and returns false. Reading and writing commence at the beginning of the file. When writing, characters at the beginning of the file are overwritten.
-
w+[b]
opens a file for reading and writing. If the file does not already exist, it is created; otherwise, it is overwritten. This method always succeeds and returns true.
-
a+[b]
opens a file for reading and appending. If the file does not already exist, it is created. This method always succeeds and returns true. Reading and appending commence at the end of the file.
When your application is finished with a file, you should close the file by calling the close
method.
Example 1. The following example opens the file info.txt
so an application can write information to it. If info.txt
does not already exist, the open
method creates it; otherwise, the open
method overwrites it. The close
method closes the file after the writeData
function is completed.
userInfo = new File("c:\data\info.txt")
userInfo.open("w")
writeData()
userInfo.close()
Example 2. The following example opens a binary file so an application can read data from it. The application uses an if
statement to take different actions depending on whether the open
statement finds the specified file.
entryGraphic = new File("c:\data\splash.gif")
if (entryGraphic.open("rb") == true) {
displayProcedure()
}
else {
errorProcedure()
}
entryGraphic.close()
File.close
Represents the prototype for this class. You can use the prototype to add properties or methods to all instances of a class. For information on prototypes, see Function.prototype
.
Reads data from a file into a string.
read(count)
The read
method reads the specified number of characters from a file, starting from the current position of the pointer. If you attempt to read more characters than the file contains, the method reads as many characters as possible. This method moves the pointer the number of characters specified by the count
parameter. See the File
object for a description of the pointer.
The read
method returns the characters it reads as a string.
Use the read
method to read information from a text file; use the readByte
method to read data from a binary file.
The following example references the file info.txt
, which contains the string "Hello World." The first read
method starts from the beginning of the file and reads the character "H." The second read
method starts from offset six and reads the characters "World."
dataFile = new File("c:\data\info.txt")
dataFile.open("r")
write("The next character is " + dataFile.read(1) + "<BR>")
dataFile.setPosition(6)
write("The next five characters are " + dataFile.read(5) + "<BR>")
dataFile.close()
This example displays the following information:
The next character is H
The next five characters are World
File.readByte
, File.readln
, File.write
Reads the next byte from an open file and returns its numeric value.
readByte()
None.
The readByte
method reads the next byte from a file, starting from the current position of the pointer. This method moves the pointer one byte. See the File
object for a description of the pointer.
The readByte
method returns the byte it reads as a number. If the pointer is at the end of the file when you issue readByte
, the method returns -1.
Use the readByte
method to read information from a binary file. You can use the readByte
method to read from a text file, but you must use the byteToString
method to convert the value to a string. Generally it is better to use the read
method to read information from a text file.
You can use the writeByte
method to write data read by the readByte
method to a file.
This example creates a copy of a binary file. In this example, a while
loop executes until the pointer is positioned past the end of the file. While the pointer is not positioned past the end of the file, the readByte
method reads the current byte from the source file, and the writeByte
method writes it to the target file. The last readByte
method positions the pointer past the end of the file, ending the while
loop.
// Create the source File object
source = new File("c:\data\source.gif")
// If the source file opens successfully, create a target file
if (source.open("rb")) {
target = new File("c:\data\target.gif")
target.open("wb")
// Copy the source file to the target
while (!source.eof()) {
data = source.readByte()
target.writeByte(data);
}
source.close();
}
target.close()
File.read
, File.readln
, File.writeByte
Reads the current line from an open file and returns it as a string.
readln()
None
The readln
method reads the current line of characters from a file, starting from the current position of the pointer. If you attempt to read more characters than the file contains, the method reads as many characters as possible. This method moves the pointer to the beginning of the next line. See the File
object for a description of the pointer.
The readln
method returns the characters it reads as a string.
The line separator characters ("\r
" and "\n
" on Windows platforms and "\n
" on UNIX platforms) are not included in the string that the readln
method returns. The \r
character is skipped; \n
determines the actual end of the line.
Use the readln
method to read information from a text file; use the readByte
method to read data from a binary file. You can use the writeln
method to write data read by the readln
method to a file.
See File.eof
File.read
, File.readByte
, File.writeln
Positions a pointer in an open file.
setPosition(position[, reference])
Use the setPosition
method to reposition the pointer in a file. See the File
object for a description of the pointer.
The position
argument is a positive or negative integer that moves the pointer the specified number of bytes relative to the reference
argument. Position 0 represents the beginning of a file. The end of a file is indicated by fileObjectName.getLength()
.
The optional reference
argument is one of the following values, indicating the reference point for position
:
The setPosition
method returns true if it is successful; otherwise, it returns false.
The following examples refer to the file info.txt
, which contains the string "Hello World." The length of info.txt
is 11 bytes. The first example moves the pointer from the beginning of the file, and the second example moves the pointer to the same location by navigating relative to the end of the file. Both examples display the following information:
The position is 10
The next character is d
Example 1. This example moves the pointer from the beginning of the file to offset 10. Because no value for reference
is supplied, the JavaScript runtime engine assumes it is 0.
dataFile = new File("c:\data\info.txt")
dataFile.open("r")
dataFile.setPosition(10)
write("The position is " + dataFile.getPosition() + "<BR>")
write("The next character is " + dataFile.read(1) + "<P>")
dataFile.close()
Example 2. This example moves the pointer from the end of the file to offset 10.
dataFile = new File("c:\data\info.txt")
dataFile.open("r")
dataFile.setPosition(-1,2)
write("The position is " + dataFile.getPosition() + "<BR>")
write("The next character is " + dataFile.read(1) + "<P>")
dataFile.close()
File.eof
, File.getPosition
, File.open
Converts the first character of a string into a number that represents a byte.
stringToByte(string)
Use the stringToByte
and byteToString
methods to convert data between binary and ASCII formats. The stringToByte
method converts the first character of its string
argument into a number that represents a byte.
Because stringToByte
is a static method of File
, you always use it as File.stringToByte()
, rather than as a method of a File
object you created.
If this method succeeds, it returns the numeric value of the first character of the input string; if it fails, it returns 0.
In the following example, the stringToByte
method is passed "Hello" as an input argument. The method converts the first character, "H," into a numeric value representing a byte.
write("The stringToByte value of Hello = " +
File.stringToByte("Hello") + "<BR>")
write("Returning that value to byteToString = " +
File.byteToString(File.stringToByte("Hello")) + "<P>")
The previous example displays the following information:
The stringToByte value of Hello = 72
Returning that value to byteToString = H
File.byteToString
Writes data from a string to a file on the server.
write(string)
The write
method writes the string specified as string
to the file specified as fileObjectName
. This method returns true if it is successful; otherwise, it returns false.
Use the write
method to write data to a text file; use the writeByte
method to write data to a binary file. You can use the read
method to read data from a file to a string for use with the write
method.
This example creates a copy of a text file, one character at a time. In this example, a while
loop executes until the pointer is positioned past the end of the file. While the pointer is not positioned past the end of the file, the read
method reads the current character from the source file, and the write
method writes it to the target file. The last read
method positions the pointer past the end of the file, ending the while
loop. See the File
object for a description of the pointer.
// Create the source File object
source = new File("c:\data\source.txt")
// If the source file opens successfully, create a target file
if (source.open("r")) {
target = new File("c:\data\target.txt")
target.open("w")
// Copy the source file to the target
while (!source.eof()) {
data = source.read(1)
target.write(data);
}
source.close();
}
target.flush()
target.close()
File.flush
, File.read
, File.writeByte
, File.writeln
Writes a byte of data to a binary file on the server.
writeByte(number)
The writeByte
method writes a byte that is specified as number
to a file that is specified as fileObjectName
. This method returns true if it is successful; otherwise, it returns false.
Use the writeByte
method to write data to a binary file; use the write
method to write data to a text file. You can use the readByte
method to read bytes of data from a file to numeric values for use with the writeByte
method.
See the example for the readByte
method.
File.flush
, File.readByte
, File.write
, File.writeln
Writes a string and a carriage return to a file on the server.
writeln(string)
The writeln
method writes the string specified as string
to the file specified as fileObjectName
. Each string is followed by the carriage return/line feed character "\n
" ("\r\n
" on Windows platforms). This method returns true if the write is successful; otherwise, it returns false.
Use the writeln
method to write data to a text file; use the writeByte
method to write data to a binary file. You can use the readln
method to read data from a file to a string for use with the writeln
method.
This example creates a copy of a text file, one line at a time. In this example, a while
loop executes until the pointer is positioned past the end of the file. While the pointer is not positioned past the end of the file, the readln
method reads the current line from the source file, and the writeln
method writes it to the target file. The last readln
method positions the pointer past the end of the file, ending the while
loop. See the File
object for a description of the pointer.
// Create the source File object
source = new File("c:\data\source.txt")
// If the source file opens successfully, create a target file
if (source.open("r")) {
target = new File("c:\data\target.txt")
target.open("w")
// Copy the source file to the target
while (!source.eof()) {
data = source.readln()
target.writeln(data);
}
source.close();
}
target.close()
Note that the readln
method ignores the carriage return/line feed characters when it reads a line from a file. The writeln
method appends these characters to the string that it writes.
File.flush
, File.readln
, File.write
, File.writeByte
Table of Contents | Previous
| Next
| Index
Last Updated: 11/13/98 10:23:08
Copyright � 1998
Netscape Communications Corporation