Skip Headers

Oracle9i Supplied PL/SQL Packages and Types Reference
Release 2 (9.2)

Part Number A96612-01
Go To Documentation Library
Home
Go To Product List
Book List
Go To Table Of Contents
Contents
Go To Index
Index

Master Index

Feedback

Go to previous page Go to next page

95
UTL_FILE

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

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.


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 using PL/SQL exceptions.


This chapter discusses the following topics:

Security

UTL_FILE 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. However, the server implementation may be running in a privileged mode, which requires a restriction on the directories that you can access.

In the past, accessible directories for the UTL_FILE functions were specified in the initialization file using the UTL_FILE_DIR parameter. However, UTL_FILE_DIR access is not recommended. It is recommended that you use the CREATE DIRECTORY feature, which replaces UTL_FILE_DIR. Directory objects offer more flexibility and granular control to the UTL_FILE application administrator, can be maintained 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.


Note:

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


File Ownership and Protections

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 nonprivileged users who need to read these files outside of PL/SQL may need access from a system administrator.

Examples (UNIX-Specific)

Given the following:

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

SQL> CREATE DIRECTORY out_dir AS '/appl/gl/user''; 
SQL> GRANT READ ON DIRECTORY user_dir TO PUBLIC; 

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

File Location Filename Accessible By

/appl/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 CREATE DIRECTORY command has been issued


Caution:

There are no user-level file permissions. UTL_FILE directory object privileges give you read and write access to all files within the specified directory.


Exceptions

Table 95-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

The ABSOLUTE_OFFSET parameter for FSEEK() is invalid; it should be greater than 0 and less than the total number of bytes in 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.

Types

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);

Go to previous page Go to next page
Oracle
Copyright © 2000, 2002 Oracle Corporation.

All Rights Reserved.
Go To Documentation Library
Home
Go To Product List
Book List
Go To Table Of Contents
Contents
Go To Index
Index

Master Index

Feedback