Server-side object | |
Implemented in | LiveWire 1.0 |
File
constructor:new File("path")
path | The path and filename in the format of the server's file system (not a URL path). |
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.
|
Allows the addition of properties to a File object.
|
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)
Function.prototype
.
Property of |
File
|
Implemented in | LiveWire 1.0 |
Method of |
File
|
Static | |
Implemented in | LiveWire 1.0 |
byteToString(number)
number | A number that represents a byte. |
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.
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 targetThis example is similar to the example used for the
while (!source.eof()) {
data = File.byteToString(source.readByte())
target.write(data);
}
source.close()
}
target.close()
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
Method of |
File
|
Implemented in | LiveWire 1.0 |
clearError()
clearError
method clears both the file error status (the value returned by the error
method) and the value returned by the eof
method.error
method.File.error
, File.eof
Method of |
File
|
Implemented in | LiveWire 1.0 |
close()
close
method. If the file is not open, the close
method fails. This method returns true if it is successful; otherwise, it returns false.open
method.File.open
, blob
Method of |
File
|
Implemented in | LiveWire 1.0 |
eof()
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.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
Method of |
File
|
Implemented in | LiveWire 1.0 |
error()
-1 if the file specified in fileObjectName
is not open
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
Method of |
File
|
Implemented in | LiveWire 1.0 |
exists()
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()
}
Method of |
File
|
Implemented in | LiveWire 1.0 |
flush()
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.
write
method for an example of the flush
method.File.write
, File.writeByte
, File.writeln
Method of |
File
|
Implemented in | LiveWire 1.0 |
getLength()
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()
Method of |
File
|
Implemented in | LiveWire 1.0 |
getPosition()
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. 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 0Example 2. This example uses
The next character is H
The new position is 1
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 10Example 3. You can position the pointer beyond the end of the file and still evaluate
The next character is d
getPosition
successfully. However, a call to eof
indicates that the pointer is beyond the end of the file.dataFile.setPosition(1,2)This example displays the following information:
write("The position is " + dataFile.getPosition() + "<BR>")
write("The value of eof is " + dataFile.eof() + "<P>")
The position is 12
The value of eof is true
File.eof
, File.open
, File.setPosition
Method of |
File
|
Implemented in | LiveWire 1.0 |
open("mode")
mode | A string specifying whether to open the file to read, write, or append, according to the list below. |
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.close
method. 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")Example 2. The following example opens a binary file so an application can read data from it. The application uses an
userInfo.open("w")
writeData()
userInfo.close()
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
Method of |
File
|
Implemented in | LiveWire 1.0 |
read(count)
count | An integer specifying the number of characters to read. |
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.
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
Method of |
File
|
Implemented in | LiveWire 1.0 |
readByte()
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.
You can use the writeByte
method to write data read by the readByte
method to a file.
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
Method of |
File
|
Implemented in | LiveWire 1.0 |
readln()
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.
File.eof
File.read
, File.readByte
, File.writeln
Method of |
File
|
Implemented in | LiveWire 1.0 |
setPosition(position, reference)
position | An integer indicating where to position the pointer. |
reference | (Optional) An integer that indicates a reference point, according to the list below. |
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()
.
setPosition
method returns true if it is successful; otherwise, it returns false.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 10Example 1. This example moves the pointer from the beginning of the file to offset 10. Because no value for
The next character is d
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
Method of |
File
|
Static | |
Implemented in | LiveWire 1.0 |
stringToByte(string)
string | A JavaScript string. |
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.
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 = " +The previous example displays the following information:
File.stringToByte("Hello") + "<BR>")
write("Returning that value to byteToString = " +
File.byteToString(File.stringToByte("Hello")) + "<P>")
The stringToByte value of Hello = 72
Returning that value to byteToString = H
File.byteToString
Method of |
File
|
Implemented in | LiveWire 1.0 |
write(string)
string | A JavaScript string. |
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.
Do not confuse the write
method of the File
object with the write
function. The write
function outputs data to the client browser; the write
method outputs data to a physical file on the server.
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
Method of |
File
|
Implemented in | LiveWire 1.0 |
writeByte(number)
number | A number that specifies a byte of data. |
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.
readByte
method.File.flush
, File.readByte
, File.write
, File.writeln
Method of |
File
|
Implemented in | LiveWire 1.0 |
writeln(string)
string | A JavaScript string. |
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.
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 targetNote that the
while (!source.eof()) {
data = source.readln()
target.writeln(data);
}
source.close();
}
target.close()
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
Last Updated: 10/31/97 16:37:22