Chapter 3 DynaScript Predefined Objects


file methods

The file object has these methods:

Close method

Syntax

file.Close( ) 

Description

Closes the file associated with the file object.

Return

Boolean. Returns true or false indicating whether the file was successfully closed.

Example

This example opens and then closes the file input.txt:

<!--SCRIPT
inputFile = new File ( "d:\\test\\input.txt", "r" );
inputFile.Close()
-->

Delete method

Syntax

file.Delete( ) 

Description

Deletes the file associated with the file object. The file must be closed to be deleted.

Return

Boolean. Returns true or false indicating whether the file was successfully deleted.

Example

This example deletes the file input.txt:

<!--SCRIPT
inputFile = new File ( "d:\\test\\input.txt", "r" );
inputFile.Close();
inputFile.Delete()
-->

GetFilePtr method

Syntax

file.GetFilePtr( )

Description

Returns the current file position. This position defines the position of the next character read or written.

Return

Integer. Position.

Example

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

Open method

Syntax

file.Open( )

Description

Opens the file specified by the name property in the read/write mode specified by the mode property.

Return

Boolean.

Example

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

Read method

Syntax

file.Read( numBytes )

Description

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.

Return

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.

Example

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

-->

ReadChar method

Syntax

file.ReadChar( )

Description

The character at the current file position is returned, and the file position is advanced by one.

Return

String. Returns the character read.

Example

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

ReadLine method

Syntax

file.ReadLine( )

Description

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.

Return

String. Returns the line read.

Example

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

Seek method

Syntax

file.Seek(offset)

Description

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.

Return

Boolean. Indicates whether the current file position was successfully set.

Example

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

Write method

Syntax

file.Write(s)

Description

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.

Return

Boolean.

Example

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

WriteLine method

Syntax

file.WriteLine(s)

Description

Identical to the Write method except that a new line is written following the written value.

Return

Boolean.

Example

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.