Chapter 3 DynaScript Predefined Objects
Provides manipulation of files through the file system. These are files external to the Web site.
For information on manipulating documents internal to the Web site, see "site object".
To use a file property:
file.propertyName
To use a file method:
file.MethodName( parameter )
To manipulate a file, a file object is created to represent the file. To create a file object, use the file constructor. The syntax to create a new file object is:
fileObj = new File( fileName, accessMode);
The specified file is opened using the access mode provided, and a file object representing the open file is returned.
This example reads information from FILE_A.DAT and adds it to the end of FILE_B.DAT, converting all tabs to spaces:
<!--SCRIPT ifile = new File( "FILE_A.DAT", "r" ); ofile = new File( "FILE_B.DAT", "a" ); for( ch = ifile.ReadChar(); !ifile.eof; ch = ifile.ReadChar() ) { if( ch == "\t" ) { ofile.Write( " " ); } else { ofile.Write( ch ); } } ifile.Close(); ofile.Close(); -->
file.eof
This property is read-only.
Set to true when an input operation attempts to read past the end of the file.
This example reads each character in the file input.txt:
<!--SCRIPT inputFile = new File( "d:\\test\\input.txt", "r" ); ch = inputFile.ReadChar (); while ( ! inputFile.eof ) { ch = inputFile.ReadChar(); } -->
file.errorNumber
This property is read-only.
A number representing the error code of the
last file method called. If an error occurred the errorNumber
is
non-zero. The values for errorNumber
and errorString
are
system dependent and may differ between NT and versions of UNIX.
This example displays the error number that results from opening a file in one mode and then opening it again in a different mode without closing the first instance:
<!--SCRIPT inputFile = new File( "d:\\test\\input.txt", "r" ); ch = inputFile.ReadChar (); while ( ! inputFile.eof ) { ch = inputFile.ReadChar(); document.WriteLn( ch ); } inputFile = new File( "d:\\test\\input.txt", "w" ); document.WriteLn( "The error number is: " + inputFile.errorNumber ); -->
This example checks for an error code of 0 and returns a message if a new file is created successfully:
<!--SCRIPT //Common Values for errorNumber on NT: //0 No error //1 No such file or directory //6 Permission denied var fileName = "c:\\foo.txt"; myFile = new File( fileName, "rt" ); if( myFile.errorNumber != 0 ) { // if error was encountered: document.writeln( "Error number " + myFile.errorNumber + " was encountered: " + myFile.errorString ); } else { document.writeln( myFile.name + " accessed successfully in " + myFile.mode + " mode." ); } -->
file.errorString
This property is read-only.
A string containing an error message for the error code of the last file method called.
This example displays an error message if there is a problem with the first file method called:
<!--SCRIPT inputFile = new File( "d:\\test\\input5.txt","r" ); document.WriteLn( inputFile.errorString ); line = inputFile.ReadLine(); document.WriteLn( inputFile.GetFilePtr() ); inputFile.Close(); -->
If the file input5.txt cannot be found in the specified directory, the output looks like:
No such file or directory null
file.name
Name of the file to be manipulated (string).
This example displays the name of the file
represented by the file object myFile
:
<!--SCRIPT myFile = new File( "d:\\test\\input.txt", "r" ); document.WriteLn( myFile.name ); -->
The output from this example is:
input.txt
The access mode with which the file was opened:
Access mode | Description |
---|---|
r | Open for read-only. |
w | Open for write, the file length to zero. |
a | Open for write at end of file. |
rb | Open binary file for reading. |
rt | Open text file for reading. |
wb | Create binary file for writing. |
wt | Create text file for writing. |
ab | Open binary file for write at end of file. |
at | Open text file for write at end of file. |
r+ | Open file for update (reading/writing). |
w+ | Create file for update (reading/writing). |
a+ | Open file for update at end of file. |
rb+ | Open binary file for update (reading/writing). |
wb+ | Create binary file for update. |
ab+ | Open binary file for update at end of file. |
rt+ | Open text file for update (reading/writing). |
wt+ | Create text file for update (reading/writing). |
at+ | Open text file for update at end of file. |
Changing a file mode
Simply changing the file mode does
not change the mode of the currently opened file. A call to Close
followed by a call to Open is required to change a file mode.
file.mode
This example opens the file output.txt and appends the phrase "hello world" to the end of the file:
<!--SCRIPT outputFile = new File ( "d:\\test\\output.txt", "a" ); outputFile.Write( "hello world" ); outputFile.Close(); -->
file.Close( )
Closes the file associated with the file object.
Boolean. Returns true or false indicating whether the file was successfully closed.
This example opens and then closes the file input.txt:
<!--SCRIPT inputFile = new File ( "d:\\test\\input.txt", "r" ); inputFile.Close() -->
file.Delete( )
Deletes the file associated with the file object. The file must be closed to be deleted.
Boolean. Returns true or false indicating whether the file was successfully deleted.
This example deletes the file input.txt:
<!--SCRIPT inputFile = new File ( "d:\\test\\input.txt", "r" ); inputFile.Close(); inputFile.Delete() -->
file.GetFilePtr( )
Returns the current file position. This position defines the position of the next character read or written.
Integer. Position.
This example displays the current position within the file input.txt:
<!--SCRIPT inputFile = new File( "d:\\test\\input.txt","r" ); line = inputFile.ReadLine(); document.WriteLn( inputFile.GetFilePtr() ); inputFile.Close(); -->
file.Open( )
Opens the file specified by the name property in the read/write mode specified by the mode property.
Boolean
This example opens the file data.txt for reading, then reopens the file for writing:
<!--SCRIPT dataFile = new File ( "d:\\test\\data.txt", "r" ); dataFile.Close( ); dataFile.mode = "w"; dataFile.Open( ); -->
file.Read( numBytes )
Reads the contents of a file. numBytes specifies
the number of bytes to read. If no value is given, the entire contents
of the file are read. This method is the recommended way of reading
a binary file as ReadChar
returns
string data and ReadLine
assumes
line end characters are present.
If the file is opened in text mode, the data returned is a string. If the file was opened in binary mode, the data returned is of type binary.
This example reads the data from the input.txt file and displays it:
<!--SCRIPT inputFile = new File( "d:\\temp\\input.txt","r" ); data=inputFile.Read(); document.WriteLn( data ); -->
file.ReadChar( )
The character at the current file position is returned, and the file position is advanced by one.
String. Returns the character read.
This example displays the first character of the file input.txt:
<!--SCRIPT inputFile = new File( "d:\\test\\input.txt","r" ); document.WriteLn( inputFile.ReadChar() ); inputFile.Close(); -->
file.ReadLine( )
Reads and returns a string starting from the current file position to the next newline character or until end of file is reached. The newline character is not discarded.
String. Returns the line read.
This example displays the first line of the file input.txt:
<!--SCRIPT inputFile = new File( "d:\\test\\input.txt","r" ); document.WriteLn( inputFile.ReadLine() ); inputFile.Close(); -->
file.Seek(offset)
Changes the current file position to the position indicated by offset. This position defines the position of the next character read or written. The position of the first character in the file is 0.
Boolean. Indicates whether the current file position was successfully set.
This example displays the fifth character from the file input.txt:
<!--SCRIPT inputFile = new File( "d:\\test\\input.txt","r"); inputFile.Seek (4); document.WriteLn( inputFile.ReadChar() ); inputFile.Close(); -->
file.Write(s)
The string value of s is written at the current file position. At the end of this operation, the current file position is set just after the written value.
Boolean
This example writes "hello world" to the file d:\test\output.txt, and erases all other text in the file:
<!--SCRIPT outputFile = new File ( "d:\\test\\output.txt","w" ); outputFile.Write( "hello world" ); outputFile.Close( ); -->
file.WriteLine(s)
Identical to the Write
method
except that a new line is written following the written value.
Boolean
This example writes "hello world" to the file output.txt, and erases all other text in the file:
<!--SCRIPT outputFile = new File ( "d:\\test\\output.txt","w" ); outputFile.WriteLine( "hello world" ); outputFile.Close( ); -->
Copyright © 2001 Sybase, Inc. All rights reserved. |
![]() |