Skip Headers
Oracle® TimesTen In-Memory Database PL/SQL Packages Reference
Release 11.2.1

Part Number E14000-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

9 UTL_FILE

With the UTL_FILE package, PL/SQL programs can read and write operating system text files. UTL_FILE provides a restricted version of operating system stream file I/O.

This chapter contains the following topics:


Using UTL_FILE


Security model

In the current TimesTen release, UTL_FILE is restricted to a single directory object called UTL_FILE_TEMP. This directory object points to the install_dir/plsql/utl_file_temp directory. Access does not extend to subdirectories of this directory. In addition, access is subject to file system permission checking.

You cannot use UTL_FILE with a link, which could be used to circumvent desired access limitations. Specifying a link as the file name will cause FOPEN to fail with an error.

In TimesTen direct mode, the application owner is owner of the file. In Client/Server mode, the server owner is owner of the file.

UTL_FILE_DIR access is not supported in TimesTen.

Important:

  • Users do not have execute permission on UTL_FILE by default. To use UTL_FILE in TimesTen, an ADMIN user or instance administrator must explicitly GRANT EXECUTE permission on it, such as in the following example:

    GRANT EXECUTE ON SYS.UTL_FILE TO scott;
    
  • The privileges needed to access files in a directory object are operating system specific. UTL_FILE directory object privileges give you read and write access to all files within the specified directory.

  • Attempting to apply invalid UTL_FILE options will cause unpredictable results.


Operational notes

The file location and file name parameters are supplied to the FOPEN function as separate strings, so that the file location can be checked against the utl_file_temp directory. Together, the file location and name must represent a legal file name on the system, and the directory must be accessible. Any subdirectories of utl_file_temp are not accessible.

UTL_FILE implicitly interprets line terminators on read requests, thereby affecting the number of bytes returned on a GET_LINE call. For example, the len parameter of UTL_FILE.GET_LINE specifies the requested number of bytes of character data. The number of bytes actually returned to the user will be the least of the following:

The FOPEN max_linesize parameter must be a number in the range 1 and 32767. If unspecified, TimesTen supplies a default value of 1024. The GET_LINE len parameter must be a number in the range 1 and 32767. If unspecified, TimesTen supplies the default value of max_linesize. If max_linesize and len are defined to be different values, then the lesser value takes precedence.

UTL_FILE.GET_RAW ignores line terminators and returns the actual number of bytes requested by the GET_RAW len parameter.

When data encoded in one character set is read and Globalization Support is told (such as by means of NLS_LANG) that it is encoded in another character set, the result is indeterminate. If NLS_LANG is set, it should be the same as the database character set.


Rules and limits

Operating system-specific parameters, such as C-shell environment variables under UNIX, cannot be used in the file location or file name parameters.

UTL_FILE I/O capabilities are similar to standard operating system stream file I/O (OPEN, GET, PUT, CLOSE) capabilities, but with some limitations. For example, you call the FOPEN function to return a file handle, which you use in subsequent calls to GET_LINE or PUT to perform stream I/O to a file. When file I/O is done, you call FCLOSE to complete any output and free resources associated with the file.


Exceptions

Table 9-1 UTL_FILE package exceptions

Exception Name Description

INVALID_PATH

File location is invalid.

INVALID_MODE

The open_mode parameter in FOPEN is invalid.

INVALID_FILEHANDLE

File handle is invalid.

INVALID_OPERATION

File could not be opened or operated on as requested.

READ_ERROR

Operating system error occurred during the read operation.

WRITE_ERROR

Operating system error occurred during the write operation.

INTERNAL_ERROR

Unspecified PL/SQL error.

CHARSETMISMATCH

A file is opened using FOPEN_NCHAR, but later I/O operations use nonchar functions such as PUTF or GET_LINE.

FILE_OPEN

The requested operation failed because the file is open.

INVALID_MAXLINESIZE

The max_linesize value for FOPEN is invalid. It should be within the range 1 to 32767.

INVALID_FILENAME

The filename parameter is invalid.

ACCESS_DENIED

Permission to access to the file location is denied.

INVALID_OFFSET

Causes of the INVALID_OFFSET exception. One of the following:

  • ABSOLUTE_OFFSET is NULL and RELATIVE_OFFSET is NULL.

  • ABSOLUTE_OFFSET is less than 0.

  • Either offset caused a seek past the end of the file.

DELETE_FAILED

The requested file delete operation failed.

RENAME_FAILED

The requested file rename operation failed.


Procedures in UTL_FILE can also raise predefined PL/SQL exceptions such as NO_DATA_FOUND or VALUE_ERROR.


Examples

Example 1

This example reads from a file using the GET_LINE procedure.

DECLARE 
  V1 VARCHAR2(32767); 
  F1 UTL_FILE.FILE_TYPE; 
BEGIN 
  -- In this example MAX_LINESIZE is less than GET_LINE's length request 
  -- so number of bytes returned will be 256 or less if a line terminator is seen.
  F1 := UTL_FILE.FOPEN('utl_file_temp','u12345.tmp','R',256); 
  UTL_FILE.GET_LINE(F1,V1,32767); 
  DBMS_OUTPUT.PUT_LINE('Get line: ' || V1);
  UTL_FILE.FCLOSE(F1); 
 
  -- In this example, FOPEN's MAX_LINESIZE is NULL and defaults to 1024, 
  -- so number of bytes returned will be 1024 or less if line terminator is seen. 
  F1 := UTL_FILE.FOPEN('utl_file_temp','u12345.tmp','R'); 
  UTL_FILE.GET_LINE(F1,V1,32767); 
  DBMS_OUTPUT.PUT_LINE('Get line: ' || V1);
  UTL_FILE.FCLOSE(F1); 
 
  -- GET_LINE doesn't specify a number of bytes, so it defaults to 
  -- same value as FOPEN's MAX_LINESIZE which is NULL and defaults to 1024. 
  -- So number of bytes returned will be 1024 or less if line terminator is seen. 
  F1 := UTL_FILE.FOPEN('utl_file_temp','u12345.tmp','R'); 
  UTL_FILE.GET_LINE(F1,V1); 
  DBMS_OUTPUT.PUT_LINE('Get line: ' || V1);
  UTL_FILE.FCLOSE(F1); 
END; 

Consider the following test file, u12345.tmp, in the utl_file_temp directory:

This is line 1.
This is line 2.
This is line 3.
This is line 4.
This is line 5.

The example results in the following output, repeatedly getting the first line only:

Get line: This is line 1.
Get line: This is line 1.
Get line: This is line 1.
 
PL/SQL procedure successfully completed.

Example 2

This example appends content to the end of a file using the PUTF procedure.

declare
   handle utl_file.file_type;
   my_world  varchar2(4) := 'Zork';
begin
   handle := utl_file.fopen('utl_file_temp','u12345.tmp','a');
   utl_file.putf(handle, '\nHello, world!\nI come from %s with %s.\n', my_world, 
                         'greetings for all earthlings');
   utl_file.fflush(handle);
   utl_file.fclose(handle);
end;
/

This appends the following to file u12345.tmp in the utl_file_temp directory:

Hello, world!
I come from Zork with greetings for all earthlings.

Example 3

This procedure gets raw data from a specified file using the GET_RAW procedure. It exits when it reaches the end of the data, through its handling of No_Data_Found in the EXCEPTION processing.

CREATE OR REPLACE PROCEDURE getraw(n IN VARCHAR2) IS
  h     UTL_FILE.FILE_TYPE;
  Buf   RAW(32767);
  Amnt  CONSTANT BINARY_INTEGER := 32767;
BEGIN
  h := UTL_FILE.FOPEN('utl_file_temp', n, 'r', 32767);
  LOOP
    BEGIN
      UTL_FILE.GET_RAW(h, Buf, Amnt);
 
      -- Do something with this chunk
      DBMS_OUTPUT.PUT_LINE('This is the raw data:');
      DBMS_OUTPUT.PUT_LINE(Buf);
    EXCEPTION WHEN No_Data_Found THEN
      EXIT;
    END;
  END LOOP;
  UTL_FILE.FCLOSE (h);
END;
/

Consider the following content in file u12345.tmp in the utl_file_temp directory:

hello world!

The example produces output as follows:

Command> begin
       > getraw('u12345.tmp');
       > end;
       > /
This is the raw data:
68656C6C6F20776F726C64210A
 
PL/SQL procedure successfully completed.

Data structures

The UTL_FILE package defines a record type.

Record types


FILE_TYPE record type

The contents of FILE_TYPE are private to the UTL_FILE package. You should not reference or change components of this record.

TYPE file_type IS RECORD (
   id          BINARY_INTEGER, 
   datatype    BINARY_INTEGER,
   byte_mode   BOOLEAN);

Fields

Table 9-2 FILE_TYPE fields

Field Description

id

A numeric value indicating the internal file handle number.

datatype

Indicates whether the file is a CHAR file, NCHAR file, or other (binary).

byte_mode

Indicates whether the file was open as a binary file or as a text file.


Important:

Oracle does not guarantee the persistence of FILE_TYPE values between database sessions or within a single session. Attempts to clone file handles or use dummy file handles may have indeterminate outcomes.

Note:

Notes on data types:
  • The PLS_INTEGER and BINARY_INTEGER data types are identical. This document uses "BINARY_INTEGER" to indicate data types in reference information (such as for table types, record types, subprogram parameters, or subprogram return values), but may use either in discussion and examples.

  • The INTEGER and NUMBER(38) data types are also identical. This document uses "INTEGER" throughout.


Summary of UTL_FILE subprograms

Table 9-3 UTL_FILE Subprograms

Subprogram Description

FCLOSE procedure

Closes a file.

FCLOSE_ALL procedure

Closes all open file handles.

FCOPY procedure

Copies a contiguous portion of a file to a newly created file.

FFLUSH procedure

Physically writes all pending output to a file.

FGETATTR procedure

Reads and returns the attributes of a disk file.

FGETPOS function

Returns the current relative offset position (in bytes) within a file, in bytes.

FOPEN function

Opens a file for input or output.

FOPEN_NCHAR function

Opens a file in Unicode for input or output.

FREMOVE procedure

Deletes a disk file, assuming that you have sufficient privileges.

FRENAME procedure

Renames an existing file to a new name, similar to the UNIX mv function.

FSEEK procedure

Adjusts the file pointer forward or backward within the file by the number of bytes specified.

GET_LINE procedure

Reads text from an open file.

GET_LINE_NCHAR procedure

Reads text in Unicode from an open file.

GET_RAW function

Reads a RAW string value from a file and adjusts the file pointer ahead by the number of bytes read.

IS_OPEN function

Determines if a file handle refers to an open file.

NEW_LINE procedure

Writes one or more operating system-specific line terminators to a file.

PUT procedure

Writes a string to a file.

PUT_LINE procedure

Writes a line to a file, and so appends an operating system-specific line terminator.

PUT_LINE_NCHAR procedure

Writes a Unicode line to a file.

PUT_NCHAR procedure

Writes a Unicode string to a file.

PUT_RAW procedure

Accepts as input a RAW data value and writes the value to the output buffer.

PUTF procedure

Equivalent to PUT but with formatting.

PUTF_NCHAR procedure

Equivalent to PUT_NCHAR but with formatting.



FCLOSE procedure

This procedure closes an open file identified by a file handle.

Syntax

UTL_FILE.FCLOSE (
   file IN OUT UTL_FILE.FILE_TYPE);

Parameters

Table 9-4 FCLOSE procedure parameters

Parameter Description

file

Active file handle returned by an FOPEN or FOPEN_NCHAR call.


Usage notes

If there is buffered data yet to be written when FCLOSE runs, then you may receive a WRITE_ERROR exception when closing a file.

Examples

See "Examples".

Exceptions

WRITE_ERROR
INVALID_FILEHANDLE

FCLOSE_ALL procedure

This procedure closes all open file handles for the session. This should be used as an emergency cleanup procedure, for example, when a PL/SQL program exits on an exception.

Syntax

UTL_FILE.FCLOSE_ALL;

Usage notes

Note:

FCLOSE_ALL does not alter the state of the open file handles held by the user. This means that an IS_OPEN test on a file handle after an FCLOSE_ALL call still returns TRUE, even though the file has been closed. No further read or write operations can be performed on a file that was open before an FCLOSE_ALL.

Exceptions

WRITE_ERROR

FCOPY procedure

This procedure copies a contiguous portion of a file to a newly created file. By default, the whole file is copied if the start_line and end_line parameters are omitted. The source file is opened in read mode. The destination file is opened in write mode. A starting and ending line number can optionally be specified to select a portion from the center of the source file for copying.

Syntax

UTL_FILE.FCOPY (
   src_location    IN VARCHAR2,
   src_filename    IN VARCHAR2,
   dest_location   IN VARCHAR2,
   dest_filename   IN VARCHAR2,
   start_line      IN BINARY_INTEGER DEFAULT 1,
   end_line        IN BINARY_INTEGER DEFAULT NULL);

Parameters

Table 9-5 FCOPY procedure parameters

Parameters Description

src_location

Directory location of the source file.

src_filename

Source file to be copied.

dest_location

Destination directory where the destination file is created.

dest_filename

Destination file created from the source file.

start_line

Line number at which to begin copying. The default is 1 for the first line.

end_line

Line number at which to stop copying. The default is NULL, signifying end of file.



FFLUSH procedure

FFLUSH physically writes pending data to the file identified by the file handle. Normally, data being written to a file is buffered. The FFLUSH procedure forces the buffered data to be written to the file. The data must be terminated with a newline character.

Flushing is useful when the file must be read while still open. For example, debugging messages can be flushed to the file so that they can be read immediately.

Syntax

UTL_FILE.FFLUSH (
   file  IN UTL_FILE.FILE_TYPE);
invalid_maxlinesize  EXCEPTION;

Parameters

Table 9-6 FFLUSH procedure parameters

Parameters Description

file

Active file handle returned by an FOPEN or FOPEN_NCHAR call.


Examples

See "Examples".

Exceptions

INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR

FGETATTR procedure

This procedure reads and returns the attributes of a disk file.

Syntax

UTL_FILE.FGETATTR(
   location    IN VARCHAR2, 
   filename    IN VARCHAR2, 
   fexists     OUT BOOLEAN, 
   file_length OUT NUMBER, 
   blocksize   OUT BINARY_INTEGER);

Parameters

Table 9-7 FGETATTR procedure parameters

Parameters Description

location

Location of the source file.

filename

The name of the file to be examined.

fexists

A BOOLEAN for whether or not the file exists.

file_length

The length of the file in bytes. NULL if file does not exist.

blocksize

The file system block size in bytes. NULL if the file does not exist.



FGETPOS function

This function returns the current relative offset position within a file, in bytes.

Syntax

UTL_FILE.FGETPOS (
   file   IN utl_file.file_type)
 RETURN BINARY_INTEGER;

Parameters

Table 9-8 FGETPOS function parameters

Parameters Description

file

Active file handle returned by an FOPEN or FOPEN_NCHAR call.


Return value

FGETPOS returns the relative offset position for an open file, in bytes. It raises an exception if the file is not open. It returns 0 for the beginning of the file.

Usage notes

If the file was opened for byte mode operations, an INVALID_OPERATION exception is raised.


FOPEN function

This function opens a file. You can specify the maximum line size and have a maximum of 50 files open simultaneously. See also "FOPEN_NCHAR function".

Syntax

UTL_FILE.FOPEN (
   location     IN VARCHAR2,
   filename     IN VARCHAR2,
   open_mode    IN VARCHAR2,
   max_linesize IN BINARY_INTEGER DEFAULT 1024) 
  RETURN utl_file.file_type;

Parameters

Table 9-9 FOPEN function parameters

Parameter Description

location

Directory location of file.

filename

File name, including extension (file type), without directory path. If a directory path is given as a part of the filename, it is ignored by FOPEN. On UNIX, the filename cannot end with a slash, "/".

open_mode

Specifies how the file is opened. Modes include:

  • r - read text

  • w - write text

  • a - append text

  • rb - read byte mode

  • wb - write byte mode

  • ab - append byte mode

If you try to open a file specifying 'a' or 'ab' for open_mode but the file does not exist, the file is created in WRITE mode.

max_linesize

Maximum number of characters for each line, including the newline character, for this file (minimum value 1, maximum value 32767). If unspecified, TimesTen supplies a default value of 1024.


Return value

FOPEN returns a file handle, which must be passed to all subsequent procedures that operate on that file. The specific contents of the file handle are private to the UTL_FILE package, and individual components should not be referenced or changed by the UTL_FILE user.

Table 9-10 FOPEN Function Return Values

Return Description

file_type

Handle to open file.


Usage notes

The file location and file name parameters are supplied to the FOPEN function as separate strings, so that the file location can be checked against the utl_file_temp directory. Together, the file location and name must represent a legal filename on the system, and the directory must be accessible. Any subdirectories of utl_file_temp are not accessible.

Examples

See "Examples".

Exceptions

INVALID_PATH: File location or name was invalid.
INVALID_MODE: The open_mode string was invalid.
INVALID_OPERATION: File could not be opened as requested.
INVALID_MAXLINESIZE: Specified max_linesize is too large or too small.

FOPEN_NCHAR function

This function opens a file in national character set mode for input or output, with the maximum line size specified. You can have a maximum of 50 files open simultaneously. With this function, you can read or write a text file in Unicode instead of in the database character set.

Even though the contents of an NVARCHAR2 buffer may be AL16UTF16 or UTF8 (depending on the national character set of the database), the contents of the file are always read and written in UTF8. UTL_FILE converts between UTF8 and AL16UTF16 as necessary.

See also "FOPEN function".

Syntax

UTL_FILE.FOPEN_NCHAR (
   location     IN VARCHAR2,
   filename     IN VARCHAR2,
   open_mode    IN VARCHAR2,
   max_linesize IN BINARY_INTEGER DEFAULT 1024) 
RETURN utl_file.file_type;

Parameters

Table 9-11 FOPEN_NCHAR function parameters

Parameter Description

location

Directory location of file.

filename

File name, including extension.

open_mode

Open mode (r, w, a, rb, wb, or ab).

max_linesize

Maximum number of characters for each line, including the newline character, for this file. The minimum value is 1. The maximum is 32767.


Return value

FOPEN_NCHAR returns a file handle, which must be passed to all subsequent procedures that operate on that file. The specific contents of the file handle are private to the UTL_FILE package, and individual components should not be referenced or changed by the UTL_FILE user.

Table 9-12 FOPEN function return value

Return Description

file_type

Handle to open file.



FREMOVE procedure

This procedure deletes a disk file, assuming that you have sufficient privileges.

Syntax

UTL_FILE.FREMOVE (
   location IN VARCHAR2,
   filename IN VARCHAR2);

Parameters

Table 9-13 FREMOVE procedure parameters

Parameters Description

location

The directory location of the file.

filename

The name of the file to be deleted.


Usage notes

The FREMOVE procedure does not verify privileges before deleting a file. The operating system verifies file and directory permissions. An exception is returned on failure.


FRENAME procedure

This procedure renames an existing file.

Syntax

UTL_FILE.FRENAME (
   src_location  IN VARCHAR2,
   src_filename  IN VARCHAR2, 
   dest_location IN VARCHAR2,
   dest_filename IN VARCHAR2,
   overwrite     IN BOOLEAN DEFAULT FALSE);

Parameters

Table 9-14 FRENAME procedure parameters

Parameters Description

src_location

The directory location of the source file.

src_filename

The source file to be renamed.

dest_location

The destination directory of the destination file.

dest_filename

The new name of the file.

overwrite

Whether it is permissible to overwrite an existing file in the destination directory. The default is FALSE.


Usage notes

Permission on both the source and destination directories must be granted.


FSEEK procedure

This procedure adjusts the file pointer forward or backward within the file by the number of bytes specified.

Syntax

UTL_FILE.FSEEK (
   file            IN OUT utl_file.file_type,
   absolute_offset IN     BINARY_INTEGER DEFAULT NULL,
   relative_offset IN     BINARY_INTEGER DEFAULT NULL);

Parameters

Table 9-15 FSEEK procedure parameters

Parameters Description

file

Active file handle returned by an FOPEN or FOPEN_NCHAR call.

absolute_offset

The absolute location to which to seek, in bytes. Default is NULL.

relative_offset

The number of bytes to seek forward or backward. Use a positive integer to seek forward, a negative integer to see backward, or 0 for the current position. Default is NULL.


Usage notes


GET_LINE procedure

This procedure reads text from the open file identified by the file handle and places the text in the output buffer parameter. Text is read up to, but not including, the line terminator, or up to the end of the file, or up to the end of the len parameter. It cannot exceed the max_linesize specified in FOPEN.

Syntax

UTL_FILE.GET_LINE (
   file        IN  UTL_FILE.FILE_TYPE,
   buffer      OUT VARCHAR2,
   len         IN  BINARY_INTEGER DEFAULT NULL);

Parameters

Table 9-16 GET_LINE procedure parameters

Parameters Description

file

Active file handle returned by an FOPEN call.

If the file is not open for reading (mode r), an INVALID_OPERATION exception is raised.

buffer

Data buffer to receive the line read from the file.

len

The number of bytes read from the file. Default is NULL. If NULL, TimesTen supplies the value of max_linesize.


Usage notes

If the line does not fit in the buffer, a VALUE_ERROR exception is raised. If no text was read due to end of file, the NO_DATA_FOUND exception is raised. If the file was opened for byte mode operations, the INVALID_OPERATION exception is raised.

Because the line terminator character is not read into the buffer, reading blank lines returns empty strings.

The maximum size of the buffer parameter is 32767 bytes unless you specify a smaller size in FOPEN.If unspecified, TimesTen supplies a default value of 1024. See also "GET_LINE_NCHAR procedure".

Examples

See "Examples".

Exceptions

INVALID_FILEHANDLE
INVALID_OPERATION
READ_ERROR
NO_DATA_FOUND
VALUE_ERROR

GET_LINE_NCHAR procedure

This procedure reads text from the open file identified by the file handle and places the text in the output buffer parameter. With this function, you can read a text file in Unicode instead of in the database character set.

The file must be opened in national character set mode, and must be encoded in the UTF8 character set. The expected buffer data type is NVARCHAR2. If a variable of another data type such as NCHAR or VARCHAR2 is specified, PL/SQL will perform standard implicit conversion from NVARCHAR2 after the text is read.

See also "GET_LINE procedure".

Syntax

UTL_FILE.GET_LINE_NCHAR (
   file        IN  UTL_FILE.FILE_TYPE,
   buffer      OUT NVARCHAR2,
   len         IN  BINARY_INTEGER DEFAULT NULL);

Parameters

Table 9-17 GET_LINE_NCHAR procedure parameters

Parameters Description

file

Active file handle returned by an FOPEN_NCHAR call. The file must be open for reading (mode r). If the file was opened by FOPEN instead of FOPEN_NCHAR, a CHARSETMISMATCH exception is raised.

buffer

Data buffer to receive the line read from the file.

len

The number of bytes read from the file. Default is NULL. If NULL, TimesTen supplies the value of max_linesize.



GET_RAW function

This function reads a RAW string value from a file and adjusts the file pointer ahead by the number of bytes read. UTL_FILE.GET_RAW ignores line terminators and returns the actual number of bytes requested by the GET_RAW len parameter.

Syntax

UTL_FILE.GET_RAW (
   file    IN  utl_file.file_type, 
   buffer  OUT NOCOPY RAW, 
   len     IN  BINARY_INTEGER DEFAULT NULL);

Parameters

Table 9-18 GET_RAW function parameters

Parameters Description

file

Active file handle returned by an FOPEN or FOPEN_NCHAR call.

buffer

The RAW data.

len

The number of bytes read from the file. Default is NULL. If NULL, len is assumed to be the maximum length of RAW.


Examples

See "Examples".


IS_OPEN function

This function tests a file handle to see if it identifies an open file. IS_OPEN reports only whether a file handle represents a file that has been opened, but not yet closed. It does not guarantee that there will be no operating system errors when you attempt to use the file handle.

Syntax

UTL_FILE.IS_OPEN (
   file  IN UTL_FILE.FILE_TYPE)
  RETURN BOOLEAN;

Parameters

Table 9-19 IS_OPEN function parameters

Parameter Description

file

Active file handle returned by an FOPEN or FOPEN_NCHAR call.


Return value

TRUE or FALSE.


NEW_LINE procedure

This procedure writes one or more line terminators to the file identified by the input file handle. This procedure is separate from PUT because the line terminator is a platform-specific character or sequence of characters.

Syntax

UTL_FILE.NEW_LINE (
   file     IN UTL_FILE.FILE_TYPE,
   lines    IN BINARY_INTEGER := 1);

Parameters

Table 9-20 NEW_LINE procedure parameters

Parameters Description

file

Active file handle returned by an FOPEN or FOPEN_NCHAR call.

lines

Number of line terminators to be written to the file.


Exceptions

INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR

PUT procedure

PUT writes the text string stored in the buffer parameter to the open file identified by the file handle. The file must be open for write operations. No line terminator is appended by PUT. Use NEW_LINE to terminate the line or use PUT_LINE to write a complete line with a line terminator. Also see "PUT_NCHAR procedure".

Syntax

UTL_FILE.PUT (
   file      IN UTL_FILE.FILE_TYPE,
   buffer    IN VARCHAR2);

Parameters

Table 9-21 PUT procedure parameters

Parameters Description

file

Active file handle returned by an FOPEN_NCHAR call. The file must be open for writing.

buffer

Buffer that contains the text to be written to the file.

If the file was not opened using mode w or a, an INVALID_OPERATION exception is raised.


Usage notes

The maximum size of the buffer parameter is 32767 bytes unless you specify a smaller size in FOPEN. If unspecified, TimesTen supplies a default value of 1024. The sum of all sequential PUT calls cannot exceed 32767 without intermediate buffer flushes.

Exceptions

INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR

PUT_LINE procedure

This procedure writes the text string stored in the buffer parameter to the open file identified by the file handle. The file must be open for write operations. PUT_LINE terminates the line with the platform-specific line terminator character or characters. Also see "PUT_LINE_NCHAR procedure".

Syntax

UTL_FILE.PUT_LINE (
   file      IN UTL_FILE.FILE_TYPE,
   buffer    IN VARCHAR2,
   autoflush IN BOOLEAN DEFAULT FALSE);

Parameters

Table 9-22 PUT_LINE procedure parameters

Parameters Description

file

Active file handle returned by an FOPEN call.

buffer

Text buffer that contains the lines to be written to the file.

autoflush

Flushes the buffer to disk after the write.


Usage notes

Exceptions

INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR

PUT_LINE_NCHAR procedure

This procedure writes the text string stored in the buffer parameter to the open file identified by the file handle. With this function, you can write a text file in Unicode instead of in the database character set. This procedure is equivalent to the PUT_NCHAR procedure, except that the line separator is appended to the written text. See also "PUT_LINE procedure".

Syntax

UTL_FILE.PUT_LINE_NCHAR (
   file    IN UTL_FILE.FILE_TYPE,
   buffer  IN NVARCHAR2);

Parameters

Table 9-23 PUT_LINE_NCHAR procedure parameters

Parameters Description

file

Active file handle returned by an FOPEN_NCHAR call. The file must be open for writing.

buffer

Text buffer that contains the lines to be written to the file.


Usage notes


PUT_NCHAR procedure

This procedure writes the text string stored in the buffer parameter to the open file identified by the file handle.

With this function, you can write a text file in Unicode instead of in the database character set. The file must be opened in the national character set mode. The text string will be written in the UTF8 character set. The expected buffer data type is NVARCHAR2. If a variable of another data type is specified, PL/SQL will perform implicit conversion to NVARCHAR2 before writing the text.

See also "PUT procedure".

Syntax

UTL_FILE.PUT_NCHAR (
   file      IN UTL_FILE.FILE_TYPE,
   buffer    IN NVARCHAR2);

Parameters

Table 9-24 PUT_NCHAR procedure parameters

Parameters Description

file

Active file handle returned by an FOPEN_NCHAR call. If the file was opened by FOPEN instead of FOPEN_NCHAR, a CHARSETMISMATCH exception is raised.

buffer

Buffer that contains the text to be written to the file.

If the file was not opened using mode w or mode a, an INVALID_OPERATION exception is raised.


Usage notes

The maximum size of the buffer parameter is 32767 bytes unless you specify a smaller size in FOPEN. If unspecified, TimesTen supplies a default value of 1024. The sum of all sequential PUT calls cannot exceed 32767 without intermediate buffer flushes.


PUT_RAW procedure

This function accepts as input a RAW data value and writes the value to the output buffer.

Syntax

UTL_FILE.PUT_RAW (
   file       IN utl_file.file_type,
   buffer     IN RAW, 
   autoflush  IN BOOLEAN DEFAULT FALSE);

Parameters

Table 9-25 PUT_RAW procedure parameters

Parameters Description

file

Active file handle returned by an FOPEN or FOPEN_NCHAR call.

buffer

The RAW data written to the buffer.

autoflush

If TRUE, performs a flush after writing the value to the output buffer. The default is FALSE.


Usage notes

You can request an automatic flush of the buffer by setting the third argument to TRUE.

The maximum size of the buffer parameter is 32767 bytes unless you specify a smaller size in FOPEN. If unspecified, TimesTen supplies a default value of 1024. The sum of all sequential PUT calls cannot exceed 32767 without intermediate buffer flushes.


PUTF procedure

This procedure is a formatted PUT procedure. It works like a limited printf(). Also see "PUTF_NCHAR procedure".

Syntax

UTL_FILE.PUTF (
   file    IN UTL_FILE.FILE_TYPE,
   format  IN VARCHAR2,
   [arg1   IN VARCHAR2  DEFAULT NULL,
   . . .  
   arg5    IN VARCHAR2  DEFAULT NULL]); 

Parameters

Table 9-26 PUTF procedure parameters

Parameters Description

file

Active file handle returned by an FOPEN call.

format

Format string that can contain text as well as the formatting characters \n and %s.

arg1..arg5

From one to five operational argument strings.

Argument strings are substituted, in order, for the %s formatters in the format string.

If there are more formatters in the format parameter string than there are arguments, then an empty string is substituted for each %s for which there is no argument.


Usage notes

Examples

See "Examples".

Exceptions

INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR

PUTF_NCHAR procedure

This procedure is a formatted version of a PUT_NCHAR procedure. Using PUTF_NCHAR, you can write a text file in Unicode instead of in the database character set. It accepts a format string with formatting elements \n and %s, and up to five arguments to be substituted for consecutive instances of %s in the format string. The expected data type of the format string and the arguments is NVARCHAR2.

If variables of another data type are specified, PL/SQL will perform implicit conversion to NVARCHAR2 before formatting the text. Formatted text is written in the UTF8 character set to the file identified by the file handle. The file must be opened in the national character set mode.

Syntax

UTL_FILE.PUTF_NCHAR (
   file    IN UTL_FILE.FILE_TYPE,
   format  IN NVARCHAR2,
   [arg1   IN NVARCHAR2  DEFAULT NULL,
   . . .  
   arg5    IN NVARCHAR2  DEFAULT NULL]); 

Parameters

Table 9-27 PUTF_NCHAR procedure parameters

Parameters Description

file

Active file handle returned by an FOPEN_NCHAR call. The file must be open for reading (mode r). If the file was opened by FOPEN instead of FOPEN_NCHAR, a CHARSETMISMATCH exception is raised.

format

Format string that can contain text as well as the formatting characters \n and %s.

arg1..arg5

From one to five operational argument strings.

Argument strings are substituted, in order, for the %s formatters in the format string.

If there are more formatters in the format parameter string than there are arguments, then an empty string is substituted for each %s for which there is no argument.


Usage notes