Saturday 19 March 2011

UTL_FILE Functions and Program

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.

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

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:
  • ABSOLUTE_OFFSET = NULL and RELATIVE_OFFSET = NULL, or
  • ABSOLUTE_OFFSET < 0, or
  • 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.

  UTL_FILE Subprograms

Subprogram
Description
Closes a file
Closes all open file handles
Copies a contiguous portion of a file to a newly created file
Physically writes all pending output to a file
Reads and returns the attributes of a disk file
Returns the current relative offset position within a file, in bytes
Opens a file for input or output
Opens a file in Unicode for input or output
Deletes a disk file, assuming that you have sufficient privileges
Renames an existing file to a new name, similar to the UNIX mv function
Adjusts the file pointer forward or backward within the file by the number of bytes specified
Reads text from an open file
Reads text in Unicode from an open file
Reads a RAW string value from a file and adjusts the file pointer ahead by the number of bytes read
Determines if a file handle refers to an open file
Writes one or more operating system-specific line terminators to a file
Writes a string to a file
Writes a line to a file, and so appends an operating system-specific line terminator
Writes a Unicode line to a file
Writes a Unicode string to a file
A PUT procedure with formatting
A PUT_NCHAR procedure with formatting, and writes a Unicode string to a file, with formatting
Accepts as input a RAW data value and writes the value to the output buffer

Syntax:

UTL_FILE.FCLOSE (
   file IN OUT FILE_TYPE);

UTL_FILE.FCLOSE_ALL;

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

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


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


UTL_FILE.FGETPOS (
   fileid IN file_type)
 RETURN PLS_INTEGER;


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


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


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


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


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


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


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


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


UTL_FILE.IS_OPEN (
   file  IN FILE_TYPE)
  RETURN BOOLEAN;


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


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


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


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


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


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


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


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


Example::

DECLARE
  V1 VARCHAR2(32767);
  F1 UTL_FILE.FILE_TYPE;
BEGIN
  -- In this example MAX_LINESIZE 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_LINE(F1,V1,32767);
  UTL_FILE.FCLOSE(F1);

  -- In this example, FOPEN'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.GET_LINE(F1,V1,32767);
  UTL_FILE.FCLOSE(F1);

  -- In this example, 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.GET_LINE(F1,V1);
  UTL_FILE.FCLOSE(F1);
END;

No comments:

Post a Comment