Skip Headers

PL/SQL Packages and Types Reference
10g Release 1 (10.1)

Part Number B10802-01
Go to Documentation Home
Home

Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Feedback

Go to previous page
Previous
Go to next page
Next
< a href="../b10802.pdf">View PDF

1 55
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

UTL_FILE is available for both cli ent-side and server-side PL/SQL. Both the client (text I/O) and server implementations are subject to server-side file system permiss ion checking.

In the past, accessible directories for the UTL_FILE functions we re specified in the initialization file using the UTL_FILE_DIR parameter. However, UTL_FILE_DIR access is n ot recommended. It is recommended that you use the CREATE DIRECTORY feature, which replaces UTL_FILE_DIR. D irectory objects offer more flexibility and granular control to the UTL_FILE application administrator, can be maintaine d dynamically (that is, without shutting down the database), and are consistent with other Oracle tools. CREATE DIRECTORY privilege is granted only to SYS and SYSTEM by default.

< table class="Note" border="0" width="80%" cellpadding="0" cellspacing="0" dir="ltr" summary="This is a layout table to format a note" title="This is a layout table to format a note">
Note:

Use the CREATE DIRECTORY feature instead of UTL_FILE_DIR for directory access verification.


On UNIX systems, the owner of a file created by the FOPEN function is the owner of the shadow process running the instance. Normally, this owner is ORACLE. Files created using FOPEN are always writable and readable using the UTL_FILE subprograms, but non privileged users who need to read these files outside of P L/SQL may need access from a system administrator.


Caution:
  • The privileges needed to access files in a directory object are operating system specific. < code>UTL_FILE directory object privileges give you read and write access to all files within the specified directory.
  • Attempting to apply invalid options will give rise to unpredictable results.


Types

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

T
YPE file_type IS RECORD (
   id       BINARY_INTEGER, 
   datatype BINARY_INTEGER);

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 list of accessible directories as specified by the ALL_DIRECTORIES view of accessible directory objects. Together, the file location and name must represent a legal filename on the system, and the directory must be accessible. A subdirectory of an accessible directory is not necessarily also acce ssible; it too must be specified using a complete path name matching an ALL_DIRECTORIES object.

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

  • The GET_LINE len parameter, or
  • The number of bytes until the next line terminator character, or
  • The max_linesize parameter specified by UTL_FILE.FOPEN

The FOPEN max_linesize parameter must be a number in the range 1 and 32767. If unspecified , Oracle supplies a default value of 1024. The GET_LINE len parameter must be a number in the range 1 and 3 2767. If unspecified, Oracle 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_FIL E.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 t old (such as by means of NLS_LANG) that it is encoded in another character set, the result is indeterminate. If NL S_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 n ame parameters.

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


Note:

The UTL_FILE package i s similar to the client-side TEXT_IO package currently provided by Oracle Procedure Builder. Restrictions for a server i mplementation require some API differences between UTL_FILE and TEXT_IO. In PL/SQL file I/O, errors are ret urned using PL/SQL exceptions.



Exceptions

Table 155-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 sy stem error occurred during the read operation.

WRITE_ERROR

Operating sys tem error occurred during the write operation.

INTERNAL_ERROR

Unspecifie d PL/SQL error

CHARSETMISMATCH

A file is opened using FOPEN_NCHAR< /code>, 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 d enied.

INVA LID_OFFSET

Causes of the INVALID_OFFSET exception:

  • ABSOLUTE_OFFSET = NULL and RELATIVE_OFFSE T = NULL, or
  • ABSOLUTE_OFFSET < 0, or
  • Either offset caused a seek past the end of the file

DELETE_FAILED

< a name="996946">

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_ERR OR.


Examples

Example 1

Note:

The examples are UNIX-specific.


Given the following:

SQL> CREATE DIRECTORY log_dir AS '/appl/gl/log'; 
SQL> GRANT REA
D ON DIRECTORY log_dir TO DBA; 

SQL> CREATE DIRECTORY out_dir AS '/appl/gl/user''; 
<
a name="1004455">SQL> GRANT READ ON DIRECTORY user_dir TO PUBLIC; 

The following file locations and filenames are valid and accessible as follows:

< /thead>
File Location Filename Accessible By

/app l/gl/log

L12345.log

Users with DBA privilege

/appl/gl/user

u12345.tmp

All users

The following file locations and filenames are invalid:

File Location Filename Invalid Because

/appl/gl/log/backup

L12345.log

# subdirectories are not accessible

/APPL/gl/log

L12345.log

# directory strings must follow case sensitivity rules as required by the O/S

/appl/gl/log

backup/L1234.log

# filenames may not include portions of directory paths

/user/tmp

L12345.log

# no corresponding CREA TE DIRECTORY command has been issued

Example 2
DECLARE 
  V1 VARCHAR2(32767
); 
  F1 UTL_FILE.FILE_TYPE; 
BEGIN 
  -- In this example MAX_LINES
IZE is less than GET_LINE's length request 
  -- so the number of bytes returned will be 256 or less if a line
terminator is 
seen. 
  F1 := UTL_FILE.FOPEN('MYDIR','MYFILE','R',256); 
  UTL_FILE.GET_L
INE(F1,V1,32767); 
  UTL_FILE.FCLOSE(F1); 

  -- In this example, F
OPEN's MAX_LINESIZE is NULL and defaults to 1024, 
  -- so the number of bytes returned will be 1024 or less if
 a line terminator 
is seen. 
  F1 := UTL_FILE.FOPEN('MYDIR','MYFILE','R'); 
  UTL_FILE.G
ET_LINE(F1,V1,32767); 
  UTL_FILE.FCLOSE(F1); 

  -- In this exampl
e, GET_LINE doesn't specify a number of bytes, so it defaults 
to 
  -- the same value as FOPEN's MAX_LINESIZE
which is NULL in this case and 
defaults to 1024. 
  -- So the number of bytes returned will be 1024 or less if
 a line terminator 
is seen. 
  F1 := UTL_FILE.FOPEN('MYDIR','MYFILE','R'); 
  UTL_FILE.G
ET_LINE(F1,V1); 
  UTL_FILE.FCLOSE(F1); 
END; 

Summary of UTL_FILE Subprograms

Table 155-2  UTL_FILE Subprograms
< td class="Formal">

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

< tr class="Formal" align="left" valign="top"> < tr class="Formal" align="left" valign="top">
Subprogram Description

FCLOSE Procedure

Closes a file

FCLOSE_ALL Procedure

Closes all open file handles

FCOPY Procedure

FF LUSH Procedure

Physically writ es 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 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 yo u have sufficient privileges

< p class="TB">FRENAME Procedure

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

FSEEK Procedure

Adjusts the file po inter forward or backward within the file by the number of bytes specified

GET_LINE Procedure

Reads text from an open file

GET_LINE_NCHAR Pr ocedure

Reads text in Unicode f rom 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

Determi nes 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 f ile

PUTF Procedure

A PUT procedure with formatting

PUT_NCHAR Procedure

Writes a Unicode string to a file

PUT_RAW Function

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

PUT_LINE Procedure

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

PUT_LIN E_NCHAR Procedure

Writes a Unico de line to a file

PUTF_NCHAR Procedure

A PUT_NCHAR procedure with formatting, and writes a Unicode string to a file, with formatting


FCLOSE Procedure

This procedure closes an open file identified by a file h andle.

Syntax

UTL_FILE.FCLOSE (
   file IN OUT FILE_TYPE);
Parameters
< a name="1003305"> Table 155-3 FCLOSE Procedure Parameters
Parameter Description

file

Active file handle returned by an FOPEN or FOPEN_NCHAR cal l.

Us age 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.

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 except ion.

Syntax

UTL_FILE.FCLOSE_ALL;

Usage Notes


Note:

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


Exceptions

WRITE_ERROR


FCOPY Procedure

< a name="1003354">

This procedure copies a contiguous portion of a file to a newly created file. By default, the wh ole 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 portio n from the center of the source file for copying.

Syntax

UTL_FILE.FCOPY (
   location   IN V
ARCHAR2,
   filename   IN VARCHAR2,
   dest_dir   IN VARCHAR2,
   d
est_file  IN VARCHAR2,
   start_line IN PLS_INTEGER DEFAULT 1,
   end_line   IN PLS_INTEG
ER DEFAULT NULL);

Parameters

Table 155-4 FCOPY Procedure Parameters
< td class="Formal">

dest_dir

Parameters Descript ion

location

The directory location of the source file, a DIRECTORY_NAME from the ALL_DIRECTORIES view (case sensitive)

filename

The source file to be copied

The destination directory where the destination file is created.

dest_file

The destination file created from the source file.

start_line

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

end_line

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


FFLUSH Procedure

FFLUSH physically writes pend ing data to the file identified by the file handle. Normally, data being written to a file is buffered. The FFLUSH proce dure 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 FILE_TY
PE);
invalid_maxlinesize  EXCEPTION;

Parameters

Table 155-5 FFLUSH Procedure Parameters
Parameters Description

file

Active file handle returned by an FOPEN or FOPEN_NCHAR call.

Exceptions

INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR

FGETATTR Procedure

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

Syntax

U
TL_FILE.FGETATTR(
   location    IN VARCHAR2, 
   filename    IN VARCHAR2, 
   exists      OUT BOOLEAN, 
   file_length OUT NUMBER, 
   blocksize   OUT NUMB
ER);

Parameters

Table 155-6 FGETATT R Procedure Parameters
Parameters Description

location

Directory location of the source file, a DIRECTORY_NAME from the ALL_DIRECTORIES view (case sensitive).

filename

The name of the file to be examined.

exists

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 Funct ion

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

Syntax

UTL_FILE.FGETPOS (
   fileid IN file_type)
 RETURN PLS_INT
EGER;

Parameters

Table 155-7 FGETPO S Parameters
< /tr>
Parameters Description< /th>

fileid

The directory location of the source file

Return Values< /font>

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


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) 
  RETURN f
ile_type;

Parameters< /h3>

Table 155-8 FO PEN Function Parameters

open_mode

Return Values

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

Table 155-9 FOPEN Function Return Values
Parameter Description

location

Directory location of file. This string is a directory object name and is case sensitive. The default is uppercase. Read privileges must be granted on this directory object for the UTL_FILE user to run FOPEN.

filename

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

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 t his file. (minimum value 1, maximum value 32767). If unspecified, Oracle supplies a default value of 1024.

< thead>
Return Description

file_type

Handle to open file.

Usage Notes

The file location an d file name parameters must be supplied to the FOPEN function as quoted strings so that the file location can be checked against the list of accessible directories as specified by the ALL_DIRECTORIES view of accessible directory objects.

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 Unicode for input or output, with the maximum line size specified. You can have a maximum of 50 files open simultaneously. With this function, yo u can read or write a text file in Unicode instead of in the database charset. See also FOPEN Function.

Syntax

UTL_FILE.FOPEN_NCHAR (
   location     IN VARCHAR2,

   filename     IN VARCHAR2,
   open_mode    IN VARCHAR2,
   max_line
size IN BINARY_INTEGER) 
RETURN file_type;

Parameters

Table 155-10 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,ab).

max_linesize

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


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 155-11 FREMOVE Procedure Parameters
Parameters Description

location

The directory location of the file, a DIRECTORY_NAME from ALL_DIR ECTORIES (case sensitive)

filename

The name of the file to be d eleted

Usage Notes

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

FRENAME Procedure

This procedure renames an existing file to a new name, similar to the UNIX mv function.

Syntax

UTL_FILE.FRENAME (
   location  IN VARCHAR2,
   filename  IN VARCH
AR2, 
   dest_dir  IN VARCHAR2,
   dest_file IN VARCHAR2,
   overwr
ite IN BOOLEAN DEFAULT FALSE);

Parameters

Table 155-12 FRENAME Procedure Parameters
Parameters Description

location

The directory location of the source file, a DIRECTORY_NAME from the ALL_DIRECTORIES view (case sensitive).

filename

The source file to be renamed.

dest_dir

The destination directory of the destination file, a DIRECTORY_NAME from the ALL_DIR ECTORIES view (case sensitive).

dest_file

The new name of the f ile.

overw rite

The default is FALSE.

Usage Notes

< a name="1005113">

Permission on both the source and destination directories must be granted. You can use the overwrite parameter to specify whether or not to overwrite a file if one exists in the destination directory. The default is FALSE for no overwrite.


FSEEK Procedure

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

Syntax

UTL_FILE.FSEEK (
<
/a>   fid             IN utl_file.file_type,
   absolute_offset IN PL_INTEGER DEFAULT NULL,
<
/a>   relative_offset IN PLS_INTEGER DEFAULT NULL);

Parameters

Table 155-13 FSEEK Procedure Parameters
Param eters Description

fid

The file ID.

absolute_offset

The absolute location to which to seek; default = NULL

relative_offset

The number of bytes to see k forward or backward; positive = forward, negative integer = backward, zero = current position, default = NULL

Usage Notes

Using FSEEK, you can read previous lines in the file without first clos ing and reopening the file. You must know the number of bytes by which you want to navigate.

If relative_offset, the procedure seeks forward. If relative_offset > 0, or backward, if relativ e_offset < 0, the procedure seeks through the file by the number of relative_offset bytes specified.

If the beginning of the file is reached before the number of bytes specified, then the file pointer is placed at the beginning of the file. If the end of the file is reached before the number of bytes specified, then an INVALID _OFFSET error is raised.

If absolute_offset, the procedure seeks to an absolute location specified in bytes.


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, th e line terminator, or up to the end of the file, or up to the end of the len parameter. It cannot exceed the max_l inesize specified in FOPEN.

Syntax

UTL_FILE.GET_LINE (
   file        IN
  FILE_TYPE,
   buffer      OUT VARCHAR2,
   len         IN  PLS_INTEGER DEFAULT NULL);
<
/pre>


Parameters

Table 155-14 GET_LINE Pro cedure Parameters
Parameters Description

file

Active file handle returned by an FOPEN call.

The file must be open for reading (mode r); otherwise an INVAL ID_OPERATION exception is raised.

buffer

Data buffer to receive the line read from the file.

len

The number of bytes read from the fi le. Default is NULL. If NULL, Oracle 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 du e to end of file, the NO_DATA_FOUND exception is raised. If the file is opened for byte mode operations, the INVAL ID_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 b uffer parameter is 32767 bytes unless you specify a smaller size in FOPEN. If unspecified, Oracle supplies a defa ult value of 1024. See also "GET_LINE_NCHAR Procedure".

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, y ou can read a text file in Unicode instead of in the database charset. See also "GET_LINE_NCHAR Procedur e".

Syntax

UTL_FILE.GET_LINE_NCHAR (
   file        IN
 FILE_TYPE,
   buffer      OUT NVARCHAR2,
   len         IN  PLS_INTEGER DEFAULT NULL);
<
/pre>


Parameters

Table 155-15 GET_LINE_NCH AR Procedure Parameters
Parameters Description

file

Active file handle returned by an FO PEN_NCHAR call. The file must be open for reading (mode r). If the file is opened by FOPEN instead o f 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, Oracle supplies the value of max_lines ize.


GET_RAW Function

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

Syntax


UTL_FILE.GET_RAW (
   fid  IN  utl_file.file_type, 
   r    OUT NOCOPY RAW, 
   len  IN  PLS_INTEGER DEFAULT NULL);

Parameters

Table 155-16 GET_RAW Procedure Parameters
Pa rameters
Description

fid

The file ID.

r

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.


IS_OPEN Function

This function tests a file handle to see if it identifies an open file. IS_OPEN reports only wh ether 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 F
ILE_TYPE)
  RETURN BOOLEAN;

Parameters

Table 155-17 IS_OPEN Function Parameters
Parameter Description

file

Active f ile handle returned by an FOPEN or FOPEN_NCHAR call.

< h3 class="CMS">Return Values

TRUE or FALSE


NEW_LINE Procedure

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

Syntax

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

Parameters

Table 155-18 NEW_LINE Procedure Parameters
< table summary="" class="Formal" border="1" frame="HSIDES" rules="GROUPS" width="100%" cellpadding="3" cellspacing="0" dir="ltr" title =""> 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 Pr ocedure

PUT writes the text string stored in the buffer parameter to th e 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. S ee also "PUT_NCHAR Procedure".

Syntax

UTL_FILE.PUT (

   file      IN FILE_TYPE,
   buffer    IN VARCHAR2);

Parameters

Table 155-19 PUT Procedure Parameters
Parameters Description

file

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

buffer

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

You must have opened the file using mode w or mode a; otherwise, 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, Oracle 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


PUTF Procedure< /h2>

This procedure is a formatted PUT procedure. It works like a limited pr intf(). See also "PUTF_NCHAR Procedure".

Syntax

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

Parameters

< h5 class="TT"> Table 155-20 PUTF Pr ocedure 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, f or 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

The format string can contain any text, but the character sequence s %s and \n have special meaning.

Examples

The following example writes the lines:

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

my_world  varchar2(4)
:= 'Zork';
...
PUTF(my_handle, 'Hello, world!\nI come from %s with %s.\n',
                my_world,
                'greetings for all earthlings');

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

< h3 class="CMS">Exceptions
INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR


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 charset. See also "PUT Procedure".

Syntax

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

Parameters

Table 155-21 PUT_NCHAR Procedure Parameters
Chara cter Sequence Meaning

%s

Substitute this sequence with the string value of the next argument in the argument list.

\n

Substitute with the appropriate platform-specific line terminator.

< /tr>
Parameters Description

file

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

buffer

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

You must have opened t he file using mode w or mode a; otherwise, an INVALID_OPERATION exception is raised.

Usage Notes

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


PUT_RAW Function

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

Syntax

UTL_FILE.PU
T_RAW (
   fid       IN utl_file.file_type,
   r         IN RAW, 
   a
utoflush IN BOOLEAN DEFAULT FALSE);

Parameters

Table 155-22 PUT_RAW Procedure Parameters
Parameters Description

fid

The file ID.

r

The RAW data written to the buffer.

autoflush

If TRUE, performs a flush after writing the value to the output buffer; defau lt is FALSE.

Usage Notes

You can request an automatic flush of the buffer b y 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, Oracle supplies a default value of 1024. The sum of all sequential PUT calls cannot exceed 32767 without intermediate buffer flushes.


PUT_LINE Procedure

This procedure writes the text string stored in the buffer parameter to the open file identi fied by the file handle. The file must be open for write operations. PUT_LINE terminates the line with the platform-spec ific line terminator character or characters.

See also "PUT_LINE _NCHAR Procedure".

Syntax

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

Parameters

Table 155-23 PUT_LINE Procedure Parame ters
< /thead>
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< /h3>

The maximum size of the buffer parameter is 32767 bytes unless you specify a smaller size in FOPEN. If unspecified, Oracle 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_NCHAR Procedure

This procedu re 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 charset. See also "PUT_LINE Procedure".

Synta x

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

Parameters

Table 155-24 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

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


PUTF_NCHAR Procedure

This procedure is a formatted PUT _NCHAR procedure. Using PUTF_NCHAR, you can write a text file in Unicode instead of in the database charset. See also "PUTF_NCHAR Procedure" and "PUT_LINE Procedure".

Syntax

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

Parameters

Table 155-25 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 is opened by FOPEN instead of FOPEN_NCHAR, a CHARSETMISMATCH exceptio n is raised.

format

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

arg1..arg5

From one to five operational argument strings.

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

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

Usage Notes

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