Oracle read/write file bfilename instance

Source: Internet
Author: User

Oracle file read/write operations are familiar to everyone. The following describes the Oracle file bfilename instance for reading and writing, hoping to help you learn how to read and write Oracle files.

Create directory allows us to flexibly read and write files in Oracle databases, greatly improving the ease of use and scalability of Oracle.
Its syntax is:
CREATE [or replace] DIRECTORY directory AS 'pathname ';

This case is created as follows:
Create or replace directory exp_dir as '/tmp ';
 
After the directory is created, you can grant the read and write permissions to specific users. The specific syntax is as follows:
Grant read [, WRITE] on directory directory TO username;

For example:
Grant read, write on directory exp_dir to eygle;
 
In this case, the user eygle has the permission to read and write the directory.

Let's look at a simple test:

 
 
  1. SQL> create or replace directory UTL_FILE_DIR as '/opt/oracle/utl_file';  
  2. Directory created.SQL> declare    
  3.     fhandle utl_file.file_type;    
  4.  begin    
  5.     fhandle := utl_file.fopen('UTL_FILE_DIR', 'example.txt', 'w');    
  6.     utl_file.put_line(fhandle , 'eygle test write one');    
  7.     utl_file.put_line(fhandle , 'eygle test write two');    
  8.     utl_file.fclose(fhandle);    
  9.  end;    
  10.  /  
  11. PL/SQL procedure successfully completed.  
  12. SQL> !  
  13. [oracle@jumper 9.2.0]$ more /opt/oracle/utl_file/example.txt   
  14. eygle test write one  
  15. eygle test write two  
  16. [oracle@jumper 9.2.0]$ 

Similarly, you can use utl_file to read files:

 
 
  1. SQL> declare    
  2.     fhandle   utl_file.file_type;    
  3.     fp_buffer varchar2(4000);    
  4.   begin    
  5.     fhandle := utl_file.fopen ('UTL_FILE_DIR','example.txt', 'R');    
  6.       
  7.     utl_file.get_line (fhandle , fp_buffer );    
  8.     dbms_output.put_line(fp_buffer );    
  9.     utl_file.get_line (fhandle , fp_buffer );   
  10.     dbms_output.put_line(fp_buffer );   
  11.     utl_file.fclose(fhandle);   
  12.   end;   
  13.   /  
  14. eygle test write one  
  15. eygle test write two  
  16. PL/SQL procedure successfully completed. 

You can query dba_directories to view all directories.

 
 
  1. SQL> select * from dba_directories;  
  2. OWNER                          DIRECTORY_NAME                 DIRECTORY_PATH------------------------------ ------------------------------ ------------------------------  
  3. SYS                            UTL_FILE_DIR                   /opt/oracle/utl_file  
  4. SYS                            BDUMP_DIR                      /opt/oracle/admin/conner/bdump  
  5. SYS                            EXP_DIR                        /opt/oracle/utl_file 


You can use drop directory to delete these paths.

 
 
  1. SQL> drop directory exp_dir; Directory droppedSQL> select * from dba_directories; OWNER DIRECTORY_NAME DIRECTORY_PATH too many SYS UTL_FILE_DIR/opt/oracle/utl_fileSYS BDUMP_DIR/opt/oracle/admin/conner/bdump
  2. Create or replace directory USER_DIR as 'E: \ PLSQL \ 310 \';
  3.  
  4. DECLARE
  5. V_content VARCHAR2 (1800 );
  6. V_bfile BFILE;
  7. Amount INT;
  8. Offset INT: = 1;
  9. BEGIN
  10. V_bfile: = bfilename ('user _ dir', 'test. TXT '); -- note that the User_dir corresponds to the directory already created above.
  11. Amount: = DBMS_LOB.getlength (v_bfile );
  12. DBMS_LOB.OPEN (v_bfile );
  13. DBMS_LOB.READ (v_bfile, amount, offset, v_content );
  14. DBMS_LOB.close (v_bfile );
  15. DBMS_OUTPUT.PUT_LINE (v_content );
  16. END;

Oracle creates and deletes user instances

How to create an Oracle instance in unix

Minimum parameter requirements for Oracle instance Creation

Statement syntax for oracle time addition and subtraction

Oracle deadlock handling

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.