In this example we create /user/dm/hello.txt in HDFS, write a single line and close the file.

UTL_FILE.FILE_TYPE Type

Before you can work with a file using UTL_FILE package you have to declare a variable of UTL_FILE.FILE_TYPE. You assign a value to this variable using UTL_FILE.FOPEN function and then use it in all other functions as an argument.

Declaring UTL_FILE.FILE_TYPE variables:

DECLARE
f UTL_FILE.FILE_TYPE;
f2 UTL_FILE.FILE_TYPE;

FOPEN Function

FOPEN function opens a file.

Syntax:

file_handle: = FOPEN(directory, file, mode);

Parameters:

Parameter

Type

Description

directory

VARCHAR

Directory name

file

VARCHAR

File name

mode

VARCHAR

Open mode: 'w'

File Open Modes:

'r' - Open file for reading.

'w' - Open file for writing. If the file does not exist it is created. If the file already exists its content is overwritten.

Return Value:

FOPEN returns UTL_FILE.FILE_TYPE file handle that you have to pass to all other functions operating on the file.