You are on page 1of 29

Let us Understand UTL_FILE Package in Oracle

Sunday, December 11, 2016

Introduction External Files by Satish Kumar Yellanki

Slide No 1

Reading Files From A Host Operating System is Allowed


Only From Oracle 7.3 OR Above.
To Accomplish Tasks Related To Reading And Writing
Files To The Host Operating System, Oracle Includes A
Pre-Defined Package Called As "UTL_FILE."
"UTL_FILE" Allows PL/SQL Programs To Both Read And
Write To Any Operating System Files That Are Accessible
From The Server on Which The Database Instance is
Running.
UTL_FILE Package is Automatically Installed When
Oracle Database is Installed on The Production System.
Preparations Before Working With UTL_FILE
We Need To Work With An Entry "UTL_FILE_DIR" in
"INIT.ORA" File Which Resides in The Oracle Installed
Folder.
"UTL_FILE_DIR"
Entry Needs To Be Added To The
"INIT.ORA" With The Directory We Will Use To Access
The Files.
Syntax
UTL_FILE_DIR = DriveName:\FolderName

The Statement Makes Oracle Use The Drive And Folder


Reading
And
Writing
Operating
System
Files.
Sunday,For
December
11, 2016
Introduction
External
Files by Satish Kumar
Yellanki
Slide No 2

Once We Make Any Change To "INIT.ORA", We Need To


Restart The Database Server, Unless We Use SPFILE.
If We Want To Give The Permission To Any Folder on The
Host Operating System, We Need To Modify The Line
UTL_FILE_DIR = *
The UTL_FILE_DIR = * Can Be A Very Huge Security Risk
on Production Servers, Hence It is Used Only For
Development Purposes.
The CREATE DIRECTORY Method Provided in Higher
Versions is A Better Option Than Using The UTL_FILE_DIR
Method.
The CREATE DIRECTORY Method is Dynamic Hence We
Need Not Restart The Production Server At All And The
Files Are Consistent With Other Oracle Tools.
Directory Objects Offer More Flexibility And Granular
Control To The UTL_FILE Application Administrator.
CREATE DIRECTORY Privilege is Granted Only To SYS And
SYSTEM By Default.
There Are No User-Level File Permissions For UTL_FILE,
The Directory Object Privileges Give Us Read And Write
Access To All Files Within The Specified Directory.

Sunday, December 11, 2016

Introduction External Files by Satish Kumar Yellanki

Slide No 3

The Standard Operating System Stream File I/O


Capabilities Are Provided By UTL_FILE With Some
Limitations.
Once The Directory is Created We Set The Permissions on
The Directory Using The GRANT Command of Oracle.
O/S Permissions Are Those of The Oracle User But Not
The Schema Owner OR Connected User.
UTL_FILE I/O Capabilities Are Similar To Standard
Operating System File Stream I/O Operations.
The File Stream I/O Operations Provided By UTL_FILE

OPEN The File


GET The Data From File
PUT The Data into File
CLOSE The File

Security Aspects
UTL_FILE is Available For Both Client-Side And ServerSide PL/SQL.
The Client Operating in Text I/O Mode is Subject To
Normal Operating System File Permission Checking.
The Server Implementation May Be Running in A
Privileged Mode, Which Requires A Restriction on The
That
The End
User
CanKumar
Access.
Sunday,Directories
December 11, 2016
Introduction
External
Files by Satish
Yellanki
Slide No 4

Step 1
Create The Directory OR Folders on The Actual Operating
System Server.
The Folders OR Subfolders Should Be Created As Per The
Relative Path That is Required As Per The Project.
Step 2
Make The Oracle Database Know on Which Directory We
Are Working For Reading OR Writing Files.
Once We Specify The Working Directory For File
Operations We Should Operate Only From That Specified
Directory.
Creating The Directory
Syntax
SQL> CREATE DIRECTORY DirectoryName
AS DriveName:\FolderName;
Note
The Above Command Creates A New Directory Entry into The
Oracle Database With A Logical Name To The Physical Directory
That is Created in The Operating System.
The Logical Name From The Oracle Database Will Be Pointing To
The Physical Path of The Derve And Folder Where The File is
Stored.
Sunday, December 11, 2016
Introduction External Files by Satish Kumar Yellanki
Slide No 5

To Execute The CREATE DIRECTORY Command, We


Need To Have Privileges From The DBA.
Log on To The System Using SYS OR SYSTEM User
And Create The Directories That Are Necessary For The
Project.
Directory Metadata
SQL> DESC ALL_DIRECTORIES
SQL> SELECT *
FROM ALL_DIRECTORIES;

The Second Step We Need To Perform is Providing


Necessary Permissions To The User To Work With That
Directory.
Step 3
Granting Permissions
Syntax
SQL> GRANT READ, WRITE
ON DIRECTORY DirectoryName
TO PUBLIC;

The PUBLIC Option Makes The Directory Accessible To


Every User Connected To The System.
PUBLIC Option is Not A Good Idea, It is Better To Provide
The Grants Only To The Required Users.

Sunday, December 11, 2016

Introduction External Files by Satish Kumar Yellanki

Slide No 6

Step 4
Create A New Text File Within The Physical Folders That
Are Created.
Step 5
Locating The File in The Physical Folders, We Read The
File Using PL/SQL Program.
Illustrated Example

DECLARE
MyFile UTL_FILE.FILE_TYPE;
V_FileOutput VARCHAR2(1000);
BEGIN
MyFile := UTL_FILE.FOPEN(
'SKYESSUTLFILE',
'PLSQL12_3.SQL', R
);
UTL_FILE.GET_LINE(MyFile , V_FileOutput);
UTL_FILE.FCLOSE(MyFile);
DBMS_OUTPUT.PUT_LINE(V_FileOutput);
END;
Note
The Above Code Identifies The Actual Physical Directory Under
The OS And The Corresponding Text File To Be Read And Prints
The First Line of The Actual Text File.
Sunday, December 11, 2016

Introduction External Files by Satish Kumar Yellanki

Slide No 7

"UTL_FILE.FILE_TYPE Datatype
FILE_TYPE is A Data Type Which Points To A Particular File
Specified By The User.
Once The FILE_TYPE Successfully Points Towards The
Specific File Then We Can Proceed With Other Operations
Like Reading And Writing Upon The File.
Declarative Syntax
TYPE FILE_TYPE
IS RECORD (
ID BINARY_INTEGER,
DataType BINARY_INTEGER,
Byte_Mode BOOLEAN
);

The Contents of FILE_TYPE Are Private To The UTL_FILE


Package.
We Cannot Reference OR Change Components of This
Record.
Create An Instance Upon The FILE_TYPE Datatype
Before Operating The Actual File Containing Data.
Once The Instance is Created Upon The FILE_TYPE
Datatype, We Call The Respective Methods That Are
Provided By The Oracle Architecture For Operations.

Sunday, December 11, 2016

Introduction External Files by Satish Kumar Yellanki

Slide No 8

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.

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.

Accepts As Input A RAW Data Value And Writes The


Value To The Output Buffer.

Writes One OR More Operating System-Specific Line


Terminators To A File.

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.

A PUT_NCHAR Procedure With Formatting. Writes A


Unicode String To A File, With Formatting.

GET_RAW Function

PUT_RAW Function
NEW_LINE Procedure
PUT_LINE Procedure

PUTF_NCHAR Procedure
Sunday, December 11, 2016

Introduction External Files by Satish Kumar Yellanki

Slide No 9

Subprogram
FFLUSH Procedure
FSEEK Function
FREMOVE Function
FCOPY Function
FGETPOS Function
FGETATTR Procedure
FRENAME Function

Description

Physically Writes All Pending Output To A File.

Adjusts The File Pointer Forward OR Backward


Within The File By The Number of Bytes Specified.

Deletes A Disk File,


Sufficient Privileges.

Copies A Contiguous Portion of A File To A Newly


Created File.

Returns The Current Relative Offset Position Within


A File, in Bytes.

Reads And Returns The Attributes of A Disk File.

Renames An Existing File To A New Name, Similar To


The Unix MV Function.

Assuming

That

We

Have

Points To 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. 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, The Subdirectory Also 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.

Sunday, December 11, 2016

Introduction External Files by Satish Kumar Yellanki

Slide No 10

FOPEN Function
FOPEN Function Opens A File.
We Can Specify The Maximum Line Size And Have A
Maximum of 50 Files Open Simultaneously.
Syntax
UTL_FILE.FOPEN(
Location IN VARCHAR2,
FileName IN VARCHAR2,
Open_Mode IN VARCHAR2,
Max_Linesize IN BINARY_INTEGER
) RETURN File_Type;

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
Cannot Be Referenced or Changed By The UTL_FILE User.
File Open Modes

Append Text
AB
Append Byte Mode
R

Read Text
RB
Read Byte Mode
W

Write Text
WB December

Write
ByteIntroduction
Mode External Files by Satish Kumar Yellanki
Sunday,
11,
2016

Slide No 11

Security Model
Execute is Granted To PUBLIC Which is A Security Risk.
Revoke Execute Grant on PUBLIC After Installation.
Steps To Revoke
SQL> CONN SYS AS SYSDBA
SQL> REVOKE EXECUTE ON UTL_FILE FROM PUBLIC;

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.

IS_OPEN Function
IS_OPEN 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.
IS_OPEN Function Does Not Guarantee That There Will
Be No Operating System Errors When We Attempt To Use
The File Handle.
IS_OPEN Function Returns A Boolean Value of TRUE OR
FALSE, Returns TRUE if The File is Open.
Sunday, December 11, 2016

Introduction External Files by Satish Kumar Yellanki

Slide No 12

Syntax
UTL_FILE.IS_OPEN(
File IN FILE_TYPE
) RETURN BOOLEAN;

FCLOSE Procedure
FCLOSE Procedure Closes An Open File Identified By A
File Handle.
If There is Buffered Data Yet To Be Written When
FCLOSE Runs, Then We May Receive A WRITE_ERROR
Exception When Closing A File.
Syntax
UTL_FILE.FCLOSE(
File IN OUT FILE_TYPE
);

Exceptions
WRITE_ERROR There is Buffered Data Yet To Be Written
INVALID_FILEHANDLE The File To Be Closed is Not Available

FCLOSE_ALL Procedure
FCLOSE_ALL Procedure Closes All Open File Handles For
The Session.
FCLOSE_ALL Should Be Used As An Emergency Cleanup
Procedure.
Sunday, December 11, 2016
Introduction External Files by Satish Kumar Yellanki
Slide No 13

FCLOSE_ALL Does Not Alter The State of The Open File


Handles Held By The User.
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;

Exceptions

WRITE_ERROR

GET_LINE Procedure
GET_LINE 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.
Sunday, December 11, 2016

Introduction External Files by Satish Kumar Yellanki

Slide No 14

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 We Specify A Smaller Size in FOPEN.
The Default is Approximately 1000 Bytes, Depending on
The Platform.
Syntax
UTL_FILE.GET_LINE(

Exceptions

File
IN FILE_TYPE,
Buffer
OUT VARCHAR2,
LineSize IN NUMBER,
Len
IN PLS_INTEGER DEFAULT NULL
);

INVALID_FILEHANDLE
INVALID_OPERATION
READ_ERROR
NO_DATA_FOUND
VALUE_ERROR

GET_RAW Function
GET_RAW
Reads
A RAW
String
Sunday,
December 11, 2016 Function
Introduction
External Files
by Satish Kumar
YellankiValue From File.
Slide No 15

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

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 Function, We
Should Use NEW_LINE To Terminate The Line OR Use
PUT_LINE To Write A Complete Line.
The Maximum Size of The Buffer Parameter is 32767
Bytes Unless We Specify A Smaller Size in FOPEN.
The Default is Approximately 1000 Bytes, Depending on
The Platform.
The Sum of All Sequential PUT Calls Cannot Exceed
32767 Without Intermediate Buffer Flushes.

Sunday, December 11, 2016

Introduction External Files by Satish Kumar Yellanki

Slide No 16

Syntax

UTL_FILE.PUT(

Exceptions

File
IN FILE_TYPE,
Buffer IN VARCHAR2
);

INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR

PUT_RAW Function
PUT_RAW Function Accepts As Input A RAW Data Value
And Writes The Value To The Output Buffer.
We 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 We Specify A Smaller Size in FOPEN.
The Default is Approximately 1000 Bytes, Depending on
The Platform.
The Sum of All Sequential PUT Calls Cannot Exceed
32767 Without Intermediate Buffer Flushes.
The Raw Data Accepted By The PUT_RAW Function Can
Be Either Audio, Video OR Picture.
Sunday, December 11, 2016

Introduction External Files by Satish Kumar Yellanki

Slide No 17

Syntax

UTL_FILE. PUT_RAW(

Exceptions

FID
IN UTL_FILE.FILE_TYPE,
R
IN RAW,
AutoFlush IN BOOLEAN DEFAULT FALSE
);

INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR

PUT_LINE Procedure
PUT_LINE 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 PlatformSpecific Line Terminator Character OR Characters.
The Maximum Size of The Buffer Parameter is 32767
Bytes Unless We Specify A Smaller Size in FOPEN.
The Default is Approximately 1000 Bytes, Depending on
The Platform. The Sum of All Sequential PUT Calls
Cannot Exceed 32767 Without Intermediate Buffer
Sunday, December 11, 2016
Introduction External Files by Satish Kumar Yellanki
Slide No 18
Flushes.

Syntax

UTL_FILE.PUT_LINE(

Exceptions

File
IN FILE_TYPE,
Buffer IN VARCHAR2,
AutoFlush IN BOOLEAN DEFAULT FALSE
);

INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR

PUTF Procedure
PUTF Procedure is A Formatted PUT Procedure.
PUTF Works Like A Limited printf() Function.
The Format String Can Contain Any Text, But The
Character Sequences %s And \n Have Special Meaning.
Syntax
UTL_FILE.PUTF(

Sunday, December 11, 2016

File IN FILE_TYPE,
Format IN VARCHAR2,
[Arg1 IN VARCHAR2 DEFAULT NULL,
...
Arg5 IN VARCHAR2 DEFAULT NULL]
);
Introduction External Files by Satish Kumar Yellanki

Slide No 19

Exceptions

INVALID_FILEHANDLE
INVALID_OPERATION
WRITE_ERROR

FFLUSH Procedure
FFLUSH Physically Writes Pending Data To The File
Identified By The File Handle And 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.
Debugging Messages Can Be Flushed To The File So That
They Can Be Read Immediately.
FFLUSH Function is Equivalent To The fflush() Function
in Language Like C Language.
Syntax
UTL_FILE.FFLUSH(

File IN FILE_TYPE
);
Invalid_MaxLineSize EXCEPTION;
Sunday, December 11, 2016

Introduction External Files by Satish Kumar Yellanki

Slide No 20

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 is Provided Then 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 Location is Provided Then 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
);

Using This Function, We Can Read Previous Lines in The


Sunday, December 11, 2016
Introduction External Files by Satish Kumar Yellanki
Slide No 21
File Without First
Closing And Reopening The File.

We Must Know The Number of Bytes By Which We Want


To Navigate While Using FSEEK Function.
FREMOVE Function
FREMOVE Function Deletes A Disk File, Assuming That
We Have Sufficient Privileges.
Syntax
UTL_FILE.FREMOVE(
Location IN VARCHAR2,
FileName IN VARCHAR2
);

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
FCOPY 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.
Sunday, December 11, 2016

Introduction External Files by Satish Kumar Yellanki

Slide No 22

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

FGETPOS Function
This Function Returns The Current
Position Within A File, in Bytes.
Syntax
UTL_FILE.FGETPOS(

FileID IN FILE_TYPE
) RETURN PLS_INTEGER;

Relative

Offset

FGETPOS Returns The Relative Offset Position For An


Open File, in Bytes.
It Raises An Exception if The File is Not Open And
0 For The
Beginning
of
The
File.
Sunday, Returns
December 11, 2016
Introduction
External Files by
Satish
Kumar
Yellanki
Slide No 23

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

FRENAME Function
FRENAME 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.
We 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.
FRENAME Renames The File Permanently Hence Any
Dependencies on The Previous File May Not Operate
Sunday, December 11, 2016

Introduction External Files by Satish Kumar Yellanki

Slide No 24

Syntax
UTL_FILE.FRENAME(
Location IN Varchar2,
FileName IN VARCHAR2,
Dest_DIR IN VARCHAR2,
Dest_File IN VARCHAR2,
Overwrite IN BOOLEAN DEFAULT FALSE
);

Specifying File Locations


The Location of The File We Provide Must Have Been
Listed As An Accessible Directory in The INIT.ORA File
For The Database Instance.
Specification in INIT.ORA File
In Windows NT 'K:\SkyEssDB\Debug'
In UNIX '/Usr/SkyEssDB/Admin'
When We Pass The Location in The Call To
UTL_FILE.FOPEN, We Provide The Location Specification
As it Appears in The INIT.ORA File.
In Case-Sensitive Operating Systems, The Case of The
Location Specification in The Initialization File Must
Match That Used
in The Call To UTL_FILE.FOPEN.
Sunday, December 11, 2016
Introduction External Files by Satish Kumar Yellanki
Slide No 25

Pseudo Syntax
In Windows NT

FILE_ID := UTL_FILE.FOPEN(

In UNIX

FILE_ID := UTL_FILE.FOPEN(

'K:\ SkyEssDB\Debug'
'Trace.Lis',
'R
);

'/Usr/SkyEssDB/Admin',
'Trace.Lis',
'W
);

The Location Must Be An Explicit, Complete Path To The


File.
We Cannot Use Operating System-Specific Parameters
Such As Environment Variables in UNIX To Specify File
Locations.
UTL_FILE Restrictions And Limitations
Prior To Oracle 8.0, We Cannot Read OR Write A Line of
Text With More Than 1023 Bytes.
From Oracle 8.0 And Above, We Can Specify A Maximum
Sunday,Line
December
11, 2016
ExternalWhen
Files by Satish
Kumar
Yellanki A File.
Slide No 26
Size
of Up Introduction
To 32767
We
Open

We Cannot Delete Files Through UTL_FILE, The Best We


Can Do is Empty A File, But Still The File Will Be on The
Disk.
We Cannot Rename Files, The Best We Can Do is Copy
The Contents of The File To Another File With New
Name.
We Do Not Have Random Access To Lines in A File.
If We Want To Read The Nth
Line, We Must Read
Through The First N Lines.
If We Want To Insert A Line of Text Between N th To Mth
Lines, We Will Have To
Read The N Lines
Write Them To A New File
Write The Inserted Line of Text
Read/Write The Remainder of The File
We Cannot Change The Security on Files With UTL_FILE.
We Cannot Access Mapped Files.
We Need To Supply Real Directory Locations For Files If
We Want To Read From OR Write To Them.
UTL_FILE is A Basic Facility For Reading And Writing
Server-Side Files.
Sunday,
December 11,
2016
Introduction
External
Files by Satish
KumarWhat
Yellanki
Slide No 27
Using
UTL_FILE
We Can
Usually
Get
We Need Done

The Process Flow of UTL_FILE


Steps To Write To A File
Declare A File Handle, The Handle Serves As A Pointer To
The File For Subsequent Calls To Programs in The
UTL_FILE Package To Manipulate The Contents of The
File.
Open The File With A Call To FOPEN, Which Returns A
File Handle To The File. We Can Open A File To Read,
Replace, OR Append Text.
Write Data To The File Using The PUT, PUTF, OR
PUT_LINE Procedures.
Close The File With A Call To FCLOSE, Which Releases
Resources Associated With The File.
Steps To Read Data From A File
Declare A File Handle.
Declare A Varchar2 String Buffer That Will Receive The
Line of Data From The File.
Open The File Using FOPEN in Read Mode.
Use The GET_LINE Procedure To Read Data From The
File And into The Buffer. To Read All The Lines From A
File, We Should Execute GET_LINE in A Loop.
Sunday,
December 11,
2016 File With
Introduction
Satish Kumar Yellanki
Slide No 28
Close
The
A External
Call Files
To byFCLOSE.

Note
We Can Read Directly From A File into A Numeric OR Date
Buffer, But in This Case, The Data in The File Will Be Converted
Implicitly, And So it Must Be Compatible With The Datatype of
The Buffer.

Exceptions
Exception Name

Error Code

Reason

Access_Denied

29289

Access To The File Has Been Denied By The Operating System

CharSetMisMatch

29298

A File Is Opened Using FOPEN_NCHAR, But Later I/O Operations Use


NONCHAR Functions Such As PUTF OR GET_LINE

Delete_Failed

29291

Unable To Delete File

File_Open

File is Already open

Internal_Error

29286

Unhandled Internal Error in The UTL_FILE Package

Invalid_FileHandle

29282

File Handle Does Not Exist

Invalid_FileName

29288

A File With The Specified Name Does Not Exist in The Path

Invalid_MaxLineSize

29287

The MAX_LINESIZE Value For FOPEN() is Invalid; It Should Be Within The


Range 1 To 32767

Invalid_Mode

29281

The OPEN_MODE Parameter in FOPEN is Invalid

Invalid_Offset

29290

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

Invalid_Operation

29283

File Could Not Be Opened OR Operated on As Requested

Invalid_Path

29280

Specified Path Does Not Exist OR is Not Visible To Oracle

Read_Error

29284

Unable To Read File

Rename_Failed

29292

Unable To Rename File

Write_Error

29285

Unable To Write To File

Sunday, December 11, 2016

Introduction External Files by Satish Kumar Yellanki

Slide No 29

You might also like