DOCUMENTS 5 - PortalScripting API
Public Member Functions | List of all members
File Class Reference

The File class allows full access to files stored on the Portal Server's filesystem. More...

Public Member Functions

boolean close ()
 Close the file handle. More...
 
boolean eof ()
 Report whether the file pointer points to EOF (end of file). More...
 
String error ()
 Retrieve the error message of the last file access error as String. More...
 
File File (String pathFileName, String mode)
 The constructor has the purpose to open a file handle to the desired file. More...
 
boolean ok ()
 Report whether an error occurred while accessing the file handle. More...
 
String read (int charsNo)
 Retrieve a block of data from the file, containing a maximum of charsNo byte. More...
 
String readLine ()
 Retrieve one line of data from the file. More...
 
boolean write (number[] byteArray)
 Write binary data to the file. More...
 
boolean write (string a, string b,...)
 Write data to the file. More...
 
boolean writeBuffer (String data, int charsNo)
 Write data to the file. More...
 

Detailed Description

The File class allows full access to files stored on the Portal Server's filesystem.

Since
ELC 3.50 / otrisPORTAL 5.0

Constructor & Destructor Documentation

◆ File()

File File::File ( String  pathFileName,
String  mode 
)

The constructor has the purpose to open a file handle to the desired file.

Once created, you cannot change the access mode of the file handle. If you need to change the access mode, you would have to close the file and reopen it.

Note
File handles are so-called expensive ressources, thus it is strongly recommanded to close them as soon as possible. Refer to File.close() for further information.
Parameters
pathFileNameString value containing the complete path and filename of the desired file
modeString representing the access mode for the file handle. Allowed values are:
  • r read mode
  • r+ read mode plus write access; if the file does not yet exist, an error is raised
  • w write mode; if the file already exists, it will be completely overwritten
  • w+ write mode plus read access; if the file already exists, it will be completely overwritten
  • a write mode with append; if the file does not yet exist, it is created, otherwise the data you write to the file will be appended
  • a+ write mode with append plus read access; if the file does not yet exist, it is created, otherwise the data you write to the file will be appended
  • t open the file in text mode (ASCII 127)
  • b open the file in binary mode
Returns
File object representing a handle to a file on the server's filesystem
Since
ELC 3.50 / otrisPORTAL 5.0
Example:
// Text-Sample
var fso = new File("c:\\tmp\\test.txt", "w+t");
if (!fso.ok())
throw fso.error();
var int_val = 65;
fso.write("Hello world: ", int_val);
fso.close();
// result: test.txt: Hello world: 65
// Binary-Sample
var fso = new File("c:\\tmp\\test.txt", "w+b");
if (!fso.ok())
throw fso.error();
var int_val = 65;
fso.write("Hello world: ", int_val);
fso.close();
// result: test.txt: Hello world: A
// Binary-Sample with Byte-Array
var fso = new File("c:\\tmp\\test.txt", "w+b");
if (!fso.ok())
throw fso.error();
var byteArr = [];
byteArr.push(72);
byteArr.push(101);
byteArr.push(108);
byteArr.push(108);
byteArr.push(111);
byteArr.push(0); // 0-Byte
byteArr.push(87);
byteArr.push(111);
byteArr.push(114);
byteArr.push(108);
byteArr.push(100);
fso.write(byteArr);
fso.close();
// result: test.txt: Hello{0-Byte}World
See also
File.close()

Member Function Documentation

◆ close()

boolean File::close ( )

Close the file handle.

Note
Since file handles are so-called expensive ressources it is strongly recommanded to close each file handle you prior created in your scripts as soon as possible.
Returns
true if successful, false in case of any error
Since
ELC 3.50 / otrisPORTAL 5.0
See also
File.File(String pathFileName, String mode)

◆ eof()

boolean File::eof ( )

Report whether the file pointer points to EOF (end of file).

Returns
true if EOF, false if not
Since
ELC 3.50 / otrisPORTAL 5.0

◆ error()

String File::error ( )

Retrieve the error message of the last file access error as String.

The error message (as long there is one) and its language depend on the operating system used on the Portal Server's machine. If there is no error, the method returns null.

Returns
String with the content of the last file access error message, null in case of no error
Since
ELC 3.50 / otrisPORTAL 5.0

◆ ok()

boolean File::ok ( )

Report whether an error occurred while accessing the file handle.

Returns
true if no error occurred, false in case of any error
Since
ELC 3.50 / otrisPORTAL 5.0

◆ read()

String File::read ( int  charsNo)

Retrieve a block of data from the file, containing a maximum of charsNo byte.

After the method has been performed, the data pointer of the file handle is moved right after the block which has been read. This might as well trigger the EOF flag, if the end of file has been reached.

Parameters
charsNointeger value indicating how many characters (resp. byte in binary mode) should be read
Returns
String containing up to charsNo characters/byte of data of the file.
Since
ELC 3.50 / otrisPORTAL 5.0

◆ readLine()

String File::readLine ( )

Retrieve one line of data from the file.

This method requires to have the file opened in text mode to work flawlessly, because the end of line is recognized by the linefeed character. After the readLine() method has been performed, the data pointer of the file handle is moved to the beginning of the next line of data.

Returns
String containing one line of data of the file.
Since
ELC 3.50 / otrisPORTAL 5.0

◆ write() [1/2]

boolean File::write ( number []  byteArray)

Write binary data to the file.

This requires to have the file handle opened with write access (meaning modes r+, w/w+, a/a+) and binary mode b.

Parameters
byteArrayArray of integers containing any data you want to write to the file
Returns
true if successful, false in case of any error
Since
DOCUMENTS 4.0
Example:
// Binary-Sample with Byte-Array
var fso = new File("c:\\tmp\\test.txt", "w+b");
if (!fso.ok())
throw fso.error();
var byteArr = [];
byteArr.push(72);
byteArr.push(101);
byteArr.push(108);
byteArr.push(108);
byteArr.push(111);
byteArr.push(0); // 0-Byte
byteArr.push(87);
byteArr.push(111);
byteArr.push(114);
byteArr.push(108);
byteArr.push(100);
fso.write(byteArr);
fso.close();
// result: test.txt: Hello{0-Byte}World
See also
File.close()

◆ write() [2/2]

boolean File::write ( string  a,
string  b,
  ... 
)

Write data to the file.

This requires to have the file handle opened with write access (meaning modes r+, w/w+, a/a+). You may concatenate as many strings as you want.

Parameters
aString containing any data you want to write to the file
bString containing any data you want to write to the file
Returns
true if successful, false in case of any error
Since
ELC 3.50 / otrisPORTAL 5.0

◆ writeBuffer()

boolean File::writeBuffer ( String  data,
int  charsNo 
)

Write data to the file.

This requires to have the file handle opened with write access (meaning modes r+, w/w+, a/a+).

Parameters
dataString containing any data you want to write to the file.
charsNointeger value indicating how many characters should be written.
Returns
true if successful, false in case of any error
Since
ELC 3.50 / otrisPORTAL 5.0

This documentation refers DOCUMENTS 5.0e (2105).
Created at 11-09-2019. - © 1998-2019 otris software AG, Königswall 21, D-44137 Dortmund. support@otris.de