Chapter 3 DynaScript Predefined Objects
The file
object
has these methods:
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 © 1999 Sybase, Inc. All rights reserved. |
![]() |