JavaScript手册 
目录
此参考中包含
的内容
轻松上手
简介
操作符
语句
核心
文档
窗口
表单
浏览器
事件和
事件句柄
LiveWire
数据库服务
进程管理服务
实用工具
全局函数
LiveConnect
的Java包
索引
版权
 
【目录】 【上一页】 【下一页】 【索引】

File

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

服务器端对象
实现版本 LiveWire 1.0

创建源

The File constructor:

new File("path")

参数

path The path and filename in the format of the server's file system (not a URL 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描述 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.

属性概览

prototype Allows the addition of properties to a File object.

方法概览

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.

示例

示例 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") 示例 2. In the following example, the File object refers to a physical file with an absolute path:

userInfo = new File("c:\\data\\info.txt") 示例 3. The following example displays the name of a File object onscreen.

userInfo = new File("c:\\data\\info.txt")
write(userInfo)

属性

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.

属性源 File
实现版本 LiveWire 1.0

方法

byteToString

Converts a number that represents a byte into a string.

方法源 File
静态
实现版本 LiveWire 1.0

语法

byteToString(number)

参数

number A number that represents a byte.

描述

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描述 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

clearError

Clears the current file error status.

方法源 File
实现版本 LiveWire 1.0

语法

clearError()

参数

无。

描述

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

close

Closes an open file on the server.

方法源 File
实现版本 LiveWire 1.0

语法

close()

参数

无。

描述

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示例 for the open method.

参看

File.open, blob

eof

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

方法源 File
实现版本 LiveWire 1.0

语法

eof()

参数

无。

描述

Use the eof method to determine whether the position of the pointer is beyond the end of a file. See File for a描述 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

error

Returns the current error status.

方法源 File
实现版本 LiveWire 1.0

语法

error()

参数

返回

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

exists

Tests whether a file exists.

方法源 File
实现版本 LiveWire 1.0

语法

exists()

参数

无。

返回

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

flush

Writes the content of the internal buffer to a file.

方法源 File
实现版本 LiveWire 1.0

语法

flush()

参数

无。

描述

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

getLength

Returns the length of a file.

方法源 File
实现版本 LiveWire 1.0

语法

getLength()

参数

无。

描述

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

getPosition

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

方法源 File
实现版本 LiveWire 1.0

语法

getPosition()

参数

返回

-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描述 of the pointer. The getPosition method returns the current pointer position; the first byte in a file is byte 0.

示例

The following示例 refer to the file info.txt, which contains the string "Hello World." The length of info.txt is 11 bytes.

示例 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
示例 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
示例 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

open

Opens a file on the server.

方法源 File
实现版本 LiveWire 1.0

语法

open("mode")

参数

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

描述

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.

示例

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

read

Reads data from a file into a string.

方法源 File
实现版本 LiveWire 1.0

语法

read(count)

参数

count An integer specifying the number of characters to read.

描述

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描述 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

readByte

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

方法源 File
实现版本 LiveWire 1.0

语法

readByte()

参数

无。

描述

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描述 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

readln

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

方法源 File
实现版本 LiveWire 1.0

语法

readln()

参数

描述

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描述 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

setPosition

Positions a pointer in an open file.

方法源 File
实现版本 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.

描述

Use the setPosition method to reposition the pointer in a file. See the File object for a描述 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示例 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示例 display the following information:

The position is 10
The next character is d
示例 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() 示例 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

stringToByte

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

方法源 File
静态
实现版本 LiveWire 1.0

语法

stringToByte(string)

参数

string A JavaScript 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

write

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

方法源 File
实现版本 LiveWire 1.0

语法

write(string)

参数

string A JavaScript 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.

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.

示例

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描述 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

writeByte

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

方法源 File
实现版本 LiveWire 1.0

语法

writeByte(number)

参数

number A number that specifies a byte of data.

描述

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

writeln

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

方法源 File
实现版本 LiveWire 1.0

语法

writeln(string)

参数

string A JavaScript 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描述 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


【目录】 【上一页】 【下一页】 【索引】

回页面顶部回页面顶部