Sunday, February 22, 2009

File manipulation in oracle with UTL_FILE package -Part 2

In the post http://arjudba.blogspot.com/2009/02/file-manipulation-in-oracle-with.html I already discussed about the subprograms FOPEN, FOPEN_NCHAR, FREMOVE, FRENAME, FCOPY, FCLOSE, FCLOSE_ALL of UTL_FILE package. Those subprograms were about the basic file handling operation such as opening, closing, renaming, moving, copying files. In this post I will write about the subprograms that are used to write contents inside OS files.

1)NEW_LINE procedure: The NEW_LINE procedure writes one or more new line terminator to the file. The syntax to use this procedure is,

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

file is the name of the file handle that is open by FOPEN/FOPEN_NCHAR function.
lines is the number of line terminators written to the file.

2)PUT procedure: The PUT procedure writes the text string stored in the buffer parameter to the open file identified by the file handle. The file must be opened by FOPEN/FOPEN_NCHAR function for write operation. The syntax is,

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

The maximum size of the buffer parameter is 32767 bytes. The default value is 1024 bytes.

Note that the sum of all sequential PUT calls cannot exceed 32767 without intermediate buffer flushes.

3)PUT_LINE procedure: The PUT_LINE procedure writes the text string stored in the buffer parameter to the open file identified by the file handle. This procedure terminates the line with the line terminator. The syntax to use of this procedure is,

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

file and buffer is same as PUT procedure. Autoflash determines whether to flush to disk after write operation.

4)PUT_LINE_NCHAR procedure: The PUT_LINE_NCHAR procedure is used to write in unicode instead of database character set text string into text file. The syntax of this procedure is,

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

5)PUT_NCHAR procedure: The PUT_NCHAR is used to write in unicode instead of database character set text string into text file. The syntax for using this procedure is,

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

6)PUTF procedure: The PUTF procedure is like PUT procedure but while writing to file you can format string with the PUTF procedure. The syntax is,

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

The Format parameter can contain text as well as the formatting characters \n and %s.

The \n is the line terminator.
The %s is the substitute with the string value of the next argument in the argument list.

7)PUTF_NCHAR procedure: The PUTF_NCHAR procedure is like PUT_NCHAR procedure but while writing to file you can format string with the PUTF_NCHAR procedure. The syntax is,

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


8)PUT_RAW function:
The PUT_RAW function accepts as input a RAW data value and writes the value to the output buffer. The syntax of this function is,

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

Example with these functions and procedures

No comments:

Post a Comment