GetFile

Mon 01 January 2018

Syntax

GetFile(filename, mode [, charset] [, pathtype])

Description

Use the GetFile function to instantiate a new file object from the File class, associate it with an external file, and open the file so you can use File class methods to read from or write to it.

Any file opened for writing (using a call to the GetFile function or the File class Open method) by a PeopleCode program that runs in the Process Scheduler is automatically managed by the Report Repository.

You can use the GetFile or GetTempFile functions to access an external file, but each execution of GetFile or GetTempFile instantiates a new file object. If you plan to access only one file at a time, you need only one file object. Use GetFile or GetTempFile to instantiate a file object for the first external file you access. Then, use Open to associate the same file object with as many different external files as you want. However, if you expect to have multiple files open at the same time, you need to instantiate multiple file objects with GetFile or GetTempFile.

GetFile and Open both perform implicit commits. Therefore, the GetTempFile function has been introduced specifically to avoid these implicit database commits. GetTempFile differs from GetFile in two respects:

  • GetTempFile does not perform an implicit commit.

  • GetTempFile does not make the associated file available through the Report Repository even when the calling PeopleCode program is run through the Process Scheduler.

Therefore, GetTempFile can be a good choice when you wish to avoid implicit database commits and when you do not need to have the file managed through the Report Repository. Otherwise, GetTempFile operates exactly the same as GetFile.

See .

Parameters

Field or Control

Definition

filespec

Specify the name, and optionally, the path, of the file you want to open.

mode

A string indicating how you want to access the file. The mode can be one of the following:

  • "R" (Read mode): opens the file for reading, starting at the beginning.

  • "W" (Write mode): opens the file for writing.

    Warning! When you specify Write mode, any existing content in the file is discarded.

  • "A" (Append mode): opens the file for writing, starting at the end. Any existing content is retained.

  • "U" (Update mode): opens the file for reading or writing, starting at the beginning of the file. Any existing content is retained. Use this mode and the GetPosition and SetPosition methods to maintain checkpoints of the current read/write position in the file.

    In Update mode, any write operation clears the file of all data that follows the position you set.

    Note: Currently, the effect of the Update mode and the GetPosition and SetPosition methods is not well defined for Unicode files. Use the Update mode only on files stored with a non-Unicode character set.

  • "E" (Conditional "exist" read mode): opens the file for reading only if it exists, starting at the beginning. If it doesn’t exist, the Open method has no effect. Before attempting to read from the file, use the IsOpen property to confirm that it’s open.

  • "N" (Conditional "new" write mode): opens the file for writing, only if it doesn’t already exist. If a file by the same name already exists, the Open method has no effect. Before attempting to write to the file, use the IsOpen property to confirm that it’s open. You can insert an asterisk (*) in the file name to ensure that a new file is created. The system replaces the asterisk with numbers starting at 1 and incrementing by 1, and checks for the existence of a file by each resulting name in turn. It uses the first name for which a file doesn’t exist. In this way you can generate a set of automatically numbered files. If you insert more than one asterisk, all but the first one are discarded.

charset

A string indicating the character set you expect when you read the file, or the character set you want to use when you write to the file. You can abbreviate Unicode UCS-2 to "U" and the host operating system's default non-Unicode (sometimes referred to as the ANSI character set) to “A”. All other character sets must be spelled out in full, for example, ASCII, Big5, Shift-JIS, UTF8, or UTF8BOM.

If “A” is specified as the character set, or you do not specify a character set, the character set used is dependent on the application server configuration. On a Windows application server, the default non-Unicode character set is dependent on the Windows ANSI Codepage (ACP) which can be checked using the DOS command chcp. On a Unix application server, the default non-Unicode character set is specified in the application server configuration file, psappsrv.cfg, and can be modified using PSADMIN. You can also use a record field value to specify the character set (for example, RECORD.CHARSET.)

A list of supported character set names valid for this argument can be found in PeopleTools: Global Technology .

See .

Note: If you attempt to read data from a file using a different character set than was used to write that data to the file, the methods used generate a runtime error or the data returned is unusable.

When a file is opened for reading using the “U” charset argument, GetFile expects the file to begin with a Unicode byte order mark (BOM). This mark indicates whether the file is written in big endian order or little endian order. A BOM consisting of the hex value 0xFEFF indicates a big endian file, a BOM consisting of the hex value 0xFFEF indicates a little endian file. If the Unicode UCS-2 file being opened does not start with a BOM, an error is returned. The BOM is automatically stripped from the file when it is read into the buffers by GetFile.

When a file is opened for writing using the “U” charset argument, the appropriate Unicode BOM is automatically written to the start of the file depending on whether the application server hardware platform operates in little endian or big endian mode.

BOMs are only expected or supported for files in Unicode character sets such as UTF8, UTF8BOM, and UCS2. For consuming applications that do expect the BOM for UTF-8 files, use the UTF8BOM character set to create UTF-8 files with the BOM.

Note: For example, the UTF-8 BOM is represented by the sequence 0xEF BB BF. This sequence can be misinterpreted by a non-Unicode character set such as ISO-8859-1 and appears as ISO characters .

When working with XML documents, specify UTF8 or UTF8BOM for charset . If you are writing an XML file using a different character set, you must remember to include a character set declaration in the XML file.

In some cases, a Unicode file (UCS-2 or UTF-8) cannot be opened. An error message is displayed, when one of the following error conditions is encountered:

  • If the character set is U (that is, UCS2) and the mode is:

    • A: The file is empty or the BOM is missing or incorrect.

    • R: The BOM is missing or incorrect.

    • W: The file does not exist, or adding the BOM failed.

  • If the character set is UTF8 or UTF8BOM and the mode is:

    • R: For UTF8BOM, the BOM is missing or the file is empty; for UTF8, the file is empty.

    • A or W: For UTF8BOM only, the file does not exist or adding the BOM failed.

pathtype

If you have prepended a path to the file name, use this parameter to specify whether the path is an absolute or relative path. The valid values for this parameter are:

  • %FilePath_Relative (default)

  • %FilePath_Absolute

If you don’t specify pathtype the default is %FilePath_Relative.

If you specify a relative path, that path is appended to the path constructed from a system-chosen environment variable. A complete discussion of relative paths and environment variables is provided in documentation on the File class.

See .

If the path is an absolute path, whatever path you specify is used verbatim. You must specify a drive letter and the complete path. You can’t use any wildcards when specifying a path.

The Component Processor automatically converts platform-specific separator characters to the appropriate form for where your PeopleCode program is executing. On a Windows system, UNIX "/" separators are converted to "\", and on a UNIX system, Windows "\" separators are converted to "/".

Note: The syntax of the file path does not depend on the file system of the platform where the file is actually stored; it depends only on the platform where your PeopleCode is executing.

Note: The syntax of the file path does not depend on the file system of the platform where the file is actually stored; it depends only on the platform where your PeopleCode is executing.

Returns

A File object if successful; Null otherwise.