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 beginning of chapter Go to next page

UTL_FILE , 2 of 2


Summary of UTL_FILE Subprograms

Table 95-2 UTL_FILE Subprograms
Subprogram Description

FOPEN Function

Opens a file for input or output.

FOPEN_NCHAR Function

Opens a file in Unicode for input or output.

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

PUT Procedure

Writes a string to a file.

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.

NEW_LINE Procedure

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

PUT_LINE Procedure

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

PUT_LINE_NCHAR Procedure

Writes a Unicode line to a file.

PUTF Procedure

A PUT procedure with formatting.

PUTF_NCHAR Procedure

A PUT_NCHAR procedure with formatting. Writes a Unicode string to a file, with formatting.

FFLUSH Procedure

Physically writes all pending output to a file.

FSEEK Function

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

FREMOVE Function

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

FCOPY Function

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

FGETPOS Function

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

FGETATTR Procedure

Reads and returns the attributes of a disk file.

FRENAME Function

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


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 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 accessible; it too must be specified using a complete path name matching an ALL_DIRECTORIES object.

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


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

Parameters

Table 95-3 FOPEN Function Parameters
Parameter Description

location

Directory location of file.

filename

File name, including extension (file type), without directory path. In Unix, the filename cannot end with /.

open_mode

Specifies how the file is opened. Modes include:

r--read text

w--write text

a--append text

If you try to open a file that does not exist using a value for open_mode, then the file is created in write mode.

max_linesize

Maximum number of characters per line, including the newline character, for this file. (minimum value 1, maximum value 32767). The default is approximately 1000 bytes.

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.

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

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, you 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_linesize IN BINARY_INTEGER) 
RETURN file_type;

Parameters

Table 95-5 FOPEN_NCHAR 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).

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 95-6 IS_OPEN Function Parameters
Parameter Description

file

Active file handle returned by an FOPEN or FOPEN_NCHAR 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 95-7 FCLOSE Procedure Parameters
Parameter Description

file

Active file handle returned by an FOPEN or FOPEN_NCHAR 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 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 linesize parameter. It cannot exceed the max_linesize specified in FOPEN.

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 the buffer parameter is 32767 bytes unless you specify a smaller size in FOPEN. The default is approximately 1000 bytes, depending on your platform. See also "GET_LINE_NCHAR Procedure".

Syntax

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

Parameters

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

linesize

Specifies the maximum number of bytes to read.

len

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

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

Syntax

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

Parameters

Table 95-9 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 is 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, len is assumed to be the maximum length of RAW.

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.

Syntax

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

Parameters

Table 95-10 GET_RAW Procedure Parameters
Parameters 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.

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

The maximum size of the buffer parameter is 32767 bytes unless you specify a smaller size in FOPEN. The default is approximately 1000 bytes, depending on your platform. The sum of all sequential PUT calls cannot exceed 32767 without intermediate buffer flushes.

Syntax

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

Parameters

Table 95-11 PUT 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 exception is raised.

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

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".

The maximum size of the buffer parameter is 32767 bytes unless you specify a smaller size in FOPEN. The default is approximately 1000 bytes, depending on your platform. The sum of all sequential PUT calls cannot exceed 32767 without intermediate buffer flushes.

Syntax

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

Parameters

Table 95-12 PUT_NCHAR Procedure Parameters
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 the file using mode w or mode a; otherwise, an INVALID_OPERATION exception is raised.

PUT_RAW Function

This function accepts as input a RAW data value and writes the value to the output buffer. 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. The default is approximately 1000 bytes, depending on your platform. The sum of all sequential PUT calls cannot exceed 32767 without intermediate buffer flushes.

Syntax

UTL_FILE. PUT_RAW (
   fid       IN utl_file.file_type,
   r         IN RAW, 
   autoflush IN BOOLEAN DEFAULT FALSE);

Parameters

Table 95-13 PUT_RAW Procedure Parameters
Parameters Description

fid (IN)

The file ID.

r (IN)

The RAW data written to the buffer.

autoflush (IN)

If TRUE, performs a flush after writing the value to the output buffer; default is 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 FILE_TYPE,
   lines    IN NATURAL := 1);

Parameters

Table 95-14 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_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 of the buffer parameter is 32767 bytes unless you specify a smaller size in FOPEN. The default is approximately 1000 bytes, depending on your platform. The sum of all sequential PUT calls cannot exceed 32767 without intermediate buffer flushes.

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 95-15 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.

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

The maximum size of the buffer parameter is 32767 bytes unless you specify a smaller size in FOPEN. The default is approximately 1000 bytes, depending on your platform. The sum of all sequential PUT calls cannot exceed 32767 without intermediate buffer flushes.

Syntax

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

Parameters

Table 95-16 PUT_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 is opened by FOPEN instead of FOPEN_NCHAR, a CHARSETMISMATCH exception is raised.

buffer

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

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.

Character 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.

See also "PUTF_NCHAR Procedure".

Syntax

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

Parameters

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

PUTF_NCHAR Procedure

This procedure is a formatted PUT_NCHAR procedure. With this function, you can write a text file in Unicode instead of in the database charset. See also "PUTF Procedure". See also "PUT_LINE Procedure".

The maximum size of the buffer parameter is 32767 bytes unless you specify a smaller size in FOPEN. The default is approximately 1000 bytes, depending on your platform. The sum of all sequential PUT calls cannot exceed 32767 without intermediate buffer flushes.

Syntax

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

Parameters

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

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

Parameters

Table 95-19 FFLUSH Procedure Parameters
Parameters Description

file

Active file handle returned by an FOPEN or FOPEN_NCHAR call.

Exceptions

INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR

FSEEK Function

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

If offset, the function seeks to a byte offset. If the end of the file or the beginning of the file is reached before seeking is done, the function returns the last or first row, respectively.

If loc, the function seeks to an absolute location specified in bytes.

Syntax

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

Parameters

Table 95-20 FSEEK Procedure Parameters
Parameters Description

fid (in)

The file ID.

absolute_offset (IN)

The absolute location to which to seek; default = NULL

relative_offset (IN)

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

Notes

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

FREMOVE Function

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

Syntax

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

Parameters

Table 95-21 FREMOVE Procedure Parameters
Parameters Description

location (IN)

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

filename (IN)

The name of the file to be deleted

Notes

The FREMOVE function does not verify privileges prior to deleting the file. The O/S verifies file and directory permissions. An exception is returned on failure.

FCOPY Function

This function 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 (
   location   IN VARCHAR2,
   filename   IN VARCHAR2,
   dest_dir   IN VARCHAR2,
   dest_file  IN VARCHAR2,
   start_line IN PLS_INTEGER DEFAULT 1,
   end_line   IN PLS_INTEGER DEFAULT NULL);

Parameters

Table 95-22 FCOPY Procedure Parameters
Parameters Description

location (IN)

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

filename (IN)

The source file to be copied

dest_dir (IN)

The destination directory where the destination file is created.

dest_file (N)

The destination file created from the source file.

start_line (IN)

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

end_line (IN)

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

FGETPOS Function

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

Syntax

UTL_FILE.FGETPOS (
   fileid IN file_type)
 RETURN PLS_INTEGER;

Parameters

Table 95-23 FGETPOS Parameters
Parameters Description

fileid (IN)

The directory location of the source file

Returns

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.

FGETATTR Procedure

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

Syntax

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

Parameters

Table 95-24 FGETATTR 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 source file to be copied

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.

FRENAME Function

This function renames an existing file to a new name, similar to the Unix mv function. 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.

Syntax

UTL_FILE.FRENAME (
   location  IN VARCHAR2,
   filename  IN VARCHAR2, 
   dest_dir  IN VARCHAR2,
   dest_file IN VARCHAR2,
   overwrite IN BOOLEAN DEFAULT FALSE);

Parameters

Table 95-25 FRENAME Parameters
Parameters Description

location (IN)

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

filename (IN)

The source file to be renamed

dest_dir (IN)

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

dest_file (N)

The new name of the file.

overwrite (IN)

The default is FALSE


Go to previous page Go to beginning of chapter 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