[Contents] [Previous] [Next] [Index]

File

Lets an application interact with a physical file on the server.

Server-side object

Implemented in

LiveWire 1.0

Created by

The File constructor:

new File("path")

Parameters

path
The path and filename in the format of the server's file system (not a URL path).

Description

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.

Property Summary

prototype
Allows the addition of properties to a File object.

Method Summary

byteToString
Converts a number that represents a byte into a string.

clearError
Clears the current file error status.

close
Closes an open file on the server.

eof
Determines whether the pointer is beyond the end of an open file.

error
Returns the current error status.

exists
Tests whether a file exists.

flush
Writes the content of the internal buffer to a file.

getLength
Returns the length of a file.

getPosition
Returns the current position of the pointer in an open file.

open
Opens a file on the server.

read
Reads data from a file into a string.

readByte
Reads the next byte from an open file and returns its numeric value.

readln
Reads the current line from an open file and returns it as a string.

setPosition
Positions a pointer in an open file.

stringToByte
Converts the first character of a string into a number that represents a byte.

write
Writes data from a string to a file on the server.

writeByte
Writes a byte of data to a binary file on the server.

writeln
Writes a string and a carriage return to a file on the server.

Examples

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)

Properties

prototype

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.

Property of

File

Implemented in

LiveWire 1.0

Methods

byteToString

Converts a number that represents a byte into a string.

Method of

File

Static

Implemented in

LiveWire 1.0

Syntax

byteToString(number)

Parameters

number
A number that represents a byte.

Description

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.

Examples

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.

See also

File.stringToByte

clearError

Clears the current file error status.

Method of

File

Implemented in

LiveWire 1.0

Syntax

clearError()

Parameters

None.

Description

The clearError method clears both the file error status (the value returned by the error method) and the value returned by the eof method.

Examples

See the example for the error method.

See also

File.error, File.eof

close

Closes an open file on the server.

Method of

File

Implemented in

LiveWire 1.0

Syntax

close()

Parameters

None.

Description

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.

Examples

See the examples for the open method.

See also

File.open, blob

eof

Determines whether the pointer is beyond the end of an open file.

Method of

File

Implemented in

LiveWire 1.0

Syntax

eof()

Parameters

None.

Description

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.

Examples

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();
}

See also

File.getPosition, File.setPosition

error

Returns the current error status.

Method of

File

Implemented in

LiveWire 1.0

Syntax

error()

Parameters

None

Returns

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.

Examples

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()

See also

File.clearError

exists

Tests whether a file exists.

Method of

File

Implemented in

LiveWire 1.0

Syntax

exists()

Parameters

None.

Returns

True if the file exists; otherwise, false.

Examples

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()
}

flush

Writes the content of the internal buffer to a file.

Method of

File

Implemented in

LiveWire 1.0

Syntax

flush()

Parameters

None.

Description

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.

Examples

See the write method for an example of the flush method.

See also

File.write, File.writeByte, File.writeln

getLength

Returns the length of a file.

Method of

File

Implemented in

LiveWire 1.0

Syntax

getLength()

Parameters

None.

Description

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.

Examples

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()

getPosition

Returns the current position of the pointer in an open file.

Method of

File

Implemented in

LiveWire 1.0

Syntax

getPosition()

Parameters

None

Returns

-1 if there is an error.

Description

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.

Examples

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

See also

File.eof, File.open, File.setPosition

open

Opens a file on the server.

Method of

File

Implemented in

LiveWire 1.0

Syntax

open("mode")

Parameters

mode
A string specifying whether to open the file to read, write, or append, according to the list below.

Description

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:

When your application is finished with a file, you should close the file by calling the close method.

Examples

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()

See also

File.close

read

Reads data from a file into a string.

Method of

File

Implemented in

LiveWire 1.0

Syntax

read(count)

Parameters

count
An integer specifying the number of characters to read.

Description

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.

Examples

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

See also

File.readByte, File.readln, File.write

readByte

Reads the next byte from an open file and returns its numeric value.

Method of

File

Implemented in

LiveWire 1.0

Syntax

readByte()

Parameters

None.

Description

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.

Examples

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()

See also

File.read, File.readln, File.writeByte

readln

Reads the current line from an open file and returns it as a string.

Method of

File

Implemented in

LiveWire 1.0

Syntax

readln()

Parameters

None

Description

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.

Examples

See File.eof

See also

File.read, File.readByte, File.writeln

setPosition

Positions a pointer in an open file.

Method of

File

Implemented in

LiveWire 1.0

Syntax

setPosition(position, reference)

Parameters

position
An integer indicating where to position the pointer.

reference
(Optional) An integer that indicates a reference point, according to the list below.

Description

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.

Examples

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()

See also

File.eof, File.getPosition, File.open

stringToByte

Converts the first character of a string into a number that represents a byte.

Method of

File

Static

Implemented in

LiveWire 1.0

Syntax

stringToByte(string)

Parameters

string
A JavaScript string.

Description

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.

Examples

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

See also

File.byteToString

write

Writes data from a string to a file on the server.

Method of

File

Implemented in

LiveWire 1.0

Syntax

write(string)

Parameters

string
A JavaScript string.

Description

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.

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.

Examples

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()

See also

File.flush, File.read, File.writeByte, File.writeln

writeByte

Writes a byte of data to a binary file on the server.

Method of

File

Implemented in

LiveWire 1.0

Syntax

writeByte(number)

Parameters

number
A number that specifies a byte of data.

Description

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.

Examples

See the example for the readByte method.

See also

File.flush, File.readByte, File.write, File.writeln

writeln

Writes a string and a carriage return to a file on the server.

Method of

File

Implemented in

LiveWire 1.0

Syntax

writeln(string)

Parameters

string
A JavaScript string.

Description

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.

Examples

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.

See also

File.flush, File.readln, File.write, File.writeByte


[Contents] [Previous] [Next] [Index]

Last Updated: 10/31/97 16:37:22


Copyright © 1997 Netscape Communications Corporation