Oracle8i Supplied Packages Reference
Release 8.1.5

A68001-01

Library

Product

Contents

Index

Prev Next

57
UTL_FILE

The UTL_FILE package lets your PL/SQL programs read and write operating system (OS) text files. It provides a restricted version of standard OS stream file input/output (I/O).

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

Security

The PL/SQL file I/O feature is available for both client side and server side PL/SQL. The client implementation (text I/O) is subject to normal operating system file permission checking, and it does not need any additional security constraints. However, the server implementation might be running in a privileged mode, and will need additional security restrictions that limit the power of this feature.


Note:

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


Server Security

Server security for PL/SQL file I/O consists of a restriction on the directories that can be accessed. Accessible directories must be specified in the instance parameter initialization file (INIT.ORA).

Specify the accessible directories for the UTL_FILE functions in the initialization file using the UTL_FILE_DIR parameter. For example:

UTL_FILE_DIR = <directory name>


Note:

The directory specification is different on different platforms.  


If the initialization file for the instance contains the line UTL_FILE_DIR = /usr/jsmith/my_app, then the directory /usr/jsmith/my_app is accessible to the FOPEN function. Note that a directory named /usr/jsmith/My_App would not be accessible on case-sensitive operating systems.

The parameter specification UTL_FILE_DIR = * has a special meaning. This entry turns off directory access checking, and it makes any directory accessible to the UTL_FILE functions.


Caution:

The '*' option should be used with great caution. Oracle does not recommend that you use this option in production systems. Also, do not include '.' (the current directory for UNIX) in the accessible directories list.

To ensure security on file systems that enable symbolic links, users must not be allowed WRITE permission to directories accessible by PL/SQL file I/O functions. The symbolic links and PL/SQL file I/O could be used to circumvent normal operating system permission checking and allow users read/write access to directories to which they would not otherwise have access.  


File Ownership and Protections

On UNIX systems, a file created by the FOPEN function has as its owner the owner of the shadow process running the instance. In the normal case, 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 PL/SQL might nned their system administrator to give them access.

Examples (UNIX-Specific)

If the parameter initialization file contains only:

UTL_FILE_DIR=/appl/gl/log
UTL_FILE_DIR=/appl/gl/out

Then, the following file locations and filenames are valid:

FILE LOCATION             FILENAME
/appl/gl/log              L10324.log
/appl/gl/out              O10324.out

But, the following file locations and filename are invalid:

FILE LOCATION             FILENAME
/appl/gl/log/backup       L10324.log          # subdirectory
/APPL/gl/log              L10324.log          # uppercase
/appl/gl/log              backup/L10324.log   # dir in name
/usr/tmp                  T10324.tmp          # not in INIT.ORA


Caution:

There are no user-level file permissions. All file locations specified by the UTL_FILE_DIR parameters are valid, for both reading and writing, for all users of the file I/O procedures. This can override operating system file permissions.  


Types

TYPE file_type IS RECORD (id BINARY_INTEGER);

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

Exceptions

Table 57-1 UTL_FILE Package Exceptions
Exception Name  Description 
INVALID_PATH
 

File location or filename was invalid.  

INVALID_MODE
 

The open_mode parameter in FOPEN was invalid.  

INVALID_FILEHANDLE
 

File handle was 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.  

In addition to these package exceptions, procedures in UTL_FILE can also raise predefined PL/SQL exceptions such as NO_DATA_FOUND or VALUE_ERROR.

Summary of Subprograms

Table 57-2 UTL_FILE Subprograms
Subprogram  Description 
FOPEN function
 

Opens a file for input or output with the default line size.  

IS_OPEN function
 

Determines if a file handle refers to an open file.  

FCLOSE procedure
 

Closes a file.  

FCLOSE_ALL procedure
 

Closes all open file handles.  

GET_LINE procedure
 

Reads a line of text from an open file.  

PUT procedure
 

Writes a line to a file. This does not append a line terminator.  

NEW_LINE procedure
 

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

PUT_LINE procedure
 

Writes a line to a file. This appends an OS-specific line terminator.  

PUTF procedure
 

A PUT procedure with formatting.  

FFLUSH procedure
 

Physically writes all pending output to a file.  

FOPEN function
 

Opens a file with the maximum line size specified.  

FOPEN function

This function opens a file for input or output. The file location must be an accessible directory, as defined in the instance's initialization parameter UTL_FILE_DIR. The complete directory path must already exist; it is not created by FOPEN.

FOPEN returns a file handle, which must be used in all subsequent I/O operations on the file.

This version of FOPEN does not take a parameter for the maximum line size. Thus, the default (which is 1023 on most systems) is used. To specify a different maximum line size, use the other, overloaded version of "FOPEN function".

You can have a maximum of 50 files open simultaneously.

Syntax

UTL_FILE.FOPEN (
   location  IN VARCHAR2,
   filename  IN VARCHAR2,
   open_mode IN VARCHAR2)
  RETURN UTL_FILE.FILE_TYPE;

Parameters

Table 57-3 FOPEN Function Parameters
Parameters  Description 
location
 

Operating system-specific string that specifies the directory in which to open the file.  

filename
 

Name of the file, including extension (file type), without any directory path information. (Under the UNIX operating system, the filename cannot be terminated with a '/'.).  

open_mode
 

String that specifies how the fie is to be opened (either upper or lower case letters can be used).

The supported values, and the UTL_FILE procedures that can be used with them are:

'r' read text (GET_LINE)

'w' write text (PUT, PUT_LINE, NEW_LINE, PUTF, FFLUSH)

'a' append text (PUT, PUT_LINE, NEW_LINE, PUTF, FFLUSH)  


Note:

If you open a file that does not exist using the 'a' value for open_mode, then the file is created in write ('w') mode.  


Returns

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.


Note:

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 in the initialization file. 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 accessible; it too must be specified using a complete path name in the initialization file.

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


Exceptions

INVALID_PATH
INVALID_MODE
INVALID_OPERATION

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 FILE_TYPE)
  RETURN BOOLEAN;

Parameters

Table 57-4 IS_OPEN Function Parameters
Parameter  Description 
file
 

Active file handle returned by an FOPEN call.  

Returns

TRUE or FALSE

Exceptions

None.

FCLOSE procedure

This procedure closes an open file identified by a file handle. If there is buffered data yet to be written when FCLOSE runs, then you may receive a WRITE_ERROR exception when closing a file.

Syntax

UTL_FILE.FCLOSE (
   file IN OUT FILE_TYPE);

Parameters

Table 57-5 FCLOSE Procedure Parameters
Parameter  Description 
file
 

Active file handle returned by an FOPEN call.  

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.


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.  


Syntax

UTL_FILE.FCLOSE_ALL;

Parameters

None.

Exceptions

WRITE_ERROR

GET_LINE procedure

This procedure reads a line of 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.

If the line does not fit in the buffer, then a VALUE_ERROR exception is raised. If no text was read due to "end of file," then the NO_DATA_FOUND exception is raised.

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

The maximum size of an input record is 1023 bytes, unless you specify a larger size in the overloaded version of FOPEN.

Syntax

UTL_FILE.GET_LINE (
   file        IN  FILE_TYPE,
   buffer      OUT VARCHAR2);

Parameters

Table 57-6 GET_LINE Procedure Parameters
Parameters  Description 
file
 

Active file handle returned by an FOPEN call.

The file must be open for reading (mode 'r'), otherwise an INVALID_OPERATION exception is raised.  

buffer
 

Data buffer to receive the line read from the file.  

Exceptions

INVALID_FILEHANDLE
INVALID_OPERATION
READ_ERROR
NO_DATA_FOUND
VALUE_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.

The maximum size of an input record is 1023 bytes, unless you specify a larger size in the overloaded version of FOPEN.

Syntax

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

Parameters

Table 57-7 PUT Procedure Parameters
Parameters  Description 
file
 

Active file handle returned by an FOPEN call.  

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.  

Exceptions

INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR

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 FILE_TYPE,
   lines    IN NATURAL := 1);

Parameters

Table 57-8 NEW_LINE Procedure Parameters
Parameters  Description 
file
 

Active file handle returned by an FOPEN call.  

lines
 

Number of line terminators to be written to the file.  

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.

The maximum size for an output record is 1023 bytes, unless you specify a larger value using the overloaded version of FOPEN.

Syntax

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

Parameters

Table 57-9 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.  

Exceptions

INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR

PUTF procedure

This procedure is a formatted PUT procedure. It works like a limited printf(). The format string can contain any text, but the character sequences '%s' and '\n' have special 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.  

Syntax

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

Parameters

Table 57-10 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.  

Example

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.

Exceptions

INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR

FFLUSH procedure

FFLUSH physically writes all pending data to the file identified by the file handle. Normally, data being written to a file is buffered. The FFLUSH procedure forces any buffered data to be written to the file.

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_TYPE);
invalid_maxlinesize  EXCEPTION;

Parameters

Table 57-11 FFLUSH Procedure Parameters
Parameters  Description 
file
 

Active file handle returned by an FOPEN call.  

Exceptions

INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR

FOPEN function

This function opens a file. You can have a maximum of 50 files open simultaneously.


Note:

This version of FOPEN enables you to specify the desired maximum line size. The other version of the "FOPEN function" uses the default line size.  


Syntax

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

Parameters

Table 57-12 FOPEN Function Parameters
Parameter  Description 
location
 

Directory location of file.  

filename
 

File name (including extension).  

open_mode
 

Open mode ('r', 'w', 'a').  

max_linesize
 

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

Returns

Table 57-13 FOPEN Function Returns
Return  Description 
file_type
 

Handle to open file.  

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.



Prev

Next
Oracle
Copyright © 1999 Oracle Corporation.

All Rights Reserved.

Library

Product

Contents

Index