Oracle
  Home arrow Oracle arrow Page 3 - Reading Text Files using Oracle PL/SQL...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ORACLE

Reading Text Files using Oracle PL/SQL and UTL_FILE
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 63
    2006-04-11

    Table of Contents:
  • Reading Text Files using Oracle PL/SQL and UTL_FILE
  • How to read a text file from a file system and display the contents using PL/SQL
  • How to read a text file from a file system and display the contents using PL/SQL: discussion
  • Improving it a bit with exception handling

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Dell PowerEdge Servers

    Reading Text Files using Oracle PL/SQL and UTL_FILE - How to read a text file from a file system and display the contents using PL/SQL: discussion
    (Page 3 of 4 )

    In this section, I shall explain the PL/SQL program given in the previous section.  Let us go through it bit by bit:

    f utl_file.file_type;

    The above statement creates a simple variable "f" (can also be called the file handle), which is based on the data type "utl_file.file_type".  This is not a simple data type like integer or varchar.  It has its own in-depth meaning. 

    "UTL_FILE" is a built-in package, which comes with the Oracle database installation.  Any package in Oracle (either user-defined or built-in) contains one or more members in it.  These members (together as a unit) are generally used to constitute a complete system to facilitate a particular feature.  Similarly, "UTL_FILE" is also a package which has several members to facilitate use of the features of accessing file system.

    "utl_file.file_type" is a different data type which points to a particular file specified by the user.  Once it successfully points, we can proceed with other operations like reading, writing, and so on.  Further proceeding, we have the following:

    s varchar2(200);

    I declared a simple variable "s" to simply store a line (max. 200 chars) of information from the text file.  Further we have the following:

    f := utl_file.fopen('SAMPLEDATA','sample1.txt','R');

    The above statement is the key to the whole program.  It simply opens the file "sample1.txt" available in the "SAMPLEDATA" directory in readonly ("R") mode.  Once PL/SQL opens the file, the handle to that file gets stored in the variable "f".  Be aware that "SAMPLEDATA" is the logical directory name as discussed in the previous section (and not the physical directory existing in your operating system).  Further proceeding we have the following:

    utl_file.get_line(f,s);

    The above line simply tries to read the first line available in the file (where "f" is pointing to) and places the line in "s" (declared previously).  Once we finish all the operations with a file, we need to close it and release any memory resources (like file handles and more).  The same is achieved using the following:

    utl_file.fclose(f);

    It is highly recommended (in fact compulsory) that you close any file after finishing our operations (even though it doesn't issue any error when not closed).  And finally, we display the output back to screen using the following statement:

    dbms_output.put_line(s);

    More Oracle Articles
    More By Jagadish Chatarji


       · Hello guys. Starting a small series to work with text files using PL/SQL. Enjoy...
     

       

    ORACLE ARTICLES

    - Tuning PL/SQL Code
    - Debugging PL/SQL Code
    - Testing PL/SQL Code
    - Working With PL/SQL Code
    - Conditional Compilation for Oracle Database ...
    - Compile-Time Warnings for Oracle DB 10g
    - Compiling PL/SQL Code for an Oracle Database
    - Troubleshooting PL/SQL Code
    - Managing PL/SQL Code
    - Data Manipulation and More for HTML DB Appli...
    - Oracle Database Fundamentals
    - Adding Processes to HTML DB Applications
    - Adding Computations, Processes, and Validati...
    - Sub-templates and More with Oracle HTML DB
    - Focusing on Templates in Oracle HTML DB

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway