dbms_UTL_file

dbms_UTL_file包:
用于读写os系统文件;
使用该包时,必须为os文件建立directory目录;

create or replace directory USR_DIR as 'E:\新建文件夹';

 --1.file_type
   /*
    该方法是utl_file包,所定义的记录类型;
   */

   type file_type is record (
       id binary_integer,
       datetime binary_integer
   );

--2,FOPEN
  /*
   该函数用于打开os文件;
  */   
  utl_file.fopen(location => ,filename => ,open_mode => ) return file_type;


  declare
      handle   utl_file.file_type;
  begin
      handle:= utl_file.fopen('USR_DIR','哈哈.txt','r',1000);
      dbms_output.put_line('文件打开成功....');
  end;


--3,fopen_nchar
  /*
   该函数用于用unicode打开os文件;
  */    

  utl_file.fopen_nchar(
      location => ,filename => ,open_mode => ,max_linesize => 
  );



--4,is_open
  /*
  该函数确定文件是否已经打开
  */
  utl_file.is_open(file => );


--5,fclose
  /*
   关闭以打开的文件
  */  
  utl_file.fclose(file => );


--6,fclose
  /*
   关闭以打开的所有文件
  */   
  utl_file.fclose_all;

-- 7,get_line
  /*
  从已经打开的文件中读取内容,行内容1会被读取的输出缓冲区中;
  */ 
  declare
     handle utl_file.file_type;
     buffer varchar2(1000);
  begin
     if NOT utl_file.is_open(handle) then
        handle:= utl_file.fopen('USR_DIR','哈哈.txt','r',1000);
     end if;  
        utl_file.get_line(handle ,buffer ,1024);
        dbms_output.put_line(buffer);
  end;

-- 8,get_line_nchar
  /*
  从已经打开的文件中以unicode读取内容,行内容1会被读取的输出缓冲区中;
  */ 
  utl_file.get_line_nchar(file => ,buffer => ,len => );  

--9,get_raw
   /*
   从已经打开的文件中读取raw内容
  */

  declare
     handle utl_file.file_type;
     buffer raw(100);
  begin
     if NOT utl_file.is_open(handle) then
        handle:= utl_file.fopen('USR_DIR','哈哈.txt','r',1000);
     end if;  
        utl_file.get_raw(handle ,buffer ,100);
        dbms_output.put_line(buffer);
  end;


--10,put/put_line
  /*
  将缓冲区的数据写入os文件中
  */  

  declare
     handle utl_file.file_type;
     buffer varchar2(100);  
  begin
     handle:= utl_file.fopen('USR_DIR','哈哈.txt','w',1000);
     buffer:= '&con';
     utl_file.put(handle,buffer);
     utl_file.new_line(handle );
     buffer:= '&con2';
     utl_file.put_line(handle,buffer);
     utl_file.fclose(handle);
 end;    

 --11,put_nchar/put_line_nchar
 /*
  将缓冲区的内容以unicode码方式写入到文件
 */

  declare
     handle utl_file.file_type;
     buffer varchar2(100);  
  begin
     handle:= utl_file.fopen('USR_DIR','哈哈.txt','w',1000);
     buffer:= '&con';
     utl_file.put_nchar(handle,buffer);
     utl_file.new_line(handle );
     buffer:= '&con2';
     utl_file.put_line_nchar(handle,buffer);
     utl_file.fclose(handle);
 end;    


 --12,put_raw
 /*
  将raw缓冲区的内容写到os文件
 */  


  declare
     handle utl_file.file_type;
     buffer raw(100);  
  begin
     handle:= utl_file.fopen('USR_DIR','哈哈.txt','w',1000);
     buffer:= '&con';
     utl_file.put_raw(handle,buffer);
     utl_file.new_line(handle);
     utl_file.fclose(handle);
 end;    


--14,putf
   /*
   该过程用于特定的格式将文本写入到os文件中;
      %s -表示字符串
      \n --表示终止符;
   */  
   utl_file.putf(file => ,format => ,arg1 => ....,arg5 => );

   declare
     handle utl_file.file_type;
   begin
     handle:= utl_file.fopen('USR_DIR','哈哈.txt','w',1000);
     utl_file.putf(handle,'%s\n%s\n','&line1','&line2');
     utl_file.fclose(handle);
   end;


--15,fflush
   /*
   该过程用于将数据强制写入到os文件中;
   */   

   utl_file.fflush(file => );



--16,fseek
   /*
   该过程用于文件指针到指定位置;
   */      

    utl_file.fseek(file => ,absolute_offset => ,relative_offset => );

    declare
       handle utl_file.file_type;
    begin
         handle:= utl_file.fopen('USR_DIR','哈哈.txt','r',1000);
         dbms_output.put_line('os文件的当前位置:' || utl_file.fgetpos(handle));
         utl_file.fseek(handle,2);
         dbms_output.put_line('os文件的现在指针位置:' ||  utl_file.fgetpos(handle));
         utl_file.fclose(handle);
    end;  




 --17,fremove      

     /*
      该过程用于删除磁盘的文件
     */ 
     utl_file.fremove(location => ,filename => );

--18;fcopy
  /*
   该过程将源文件的部分或全部复制到目标文件中去;
  */   
  utl_file.fcopy
          (src_location => ,src_filename => ,
           dest_location => ,dest_filename => ,
           start_line => ,end_line => 
         );

 --19,fgetpos
   /*
   该函数返回你文件指针所偏移的位置
   */
   utl_file.fgetpos(file => );

 --20,fgetattr 

   /*
   该过程用于读取磁盘的文件,并返回属性
   */
   utl_file.fgetattr(
       location => ,filename =>,
       fexists => ,file_length => ,block_size =>  
   );

 --21,frename 

   /*
   该过程用于修改已经存在的os文件(覆盖)
   */   
   utl_file.frename
             (src_location => ,src_filename => ,
             dest_location => ,dest_filename => ,
             overwrite =>
            );
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值