oracle中utl_file包读写文件操作学习

81 篇文章 0 订阅
22 篇文章 0 订阅

在oracle中utl_file包提供了一些操作文本文件的函数和过程,学习了一下他的基本操作

1.创建directory,并给用户授权

--创建directory
create or replace directory TESTFILE as '/home/oracle/zxx/test';
--给用户授权
grant read, write on directory TESTFILE to zxx;

 

详细介绍

http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/u_file.htm

 

2.写入操作

---测试写入
 DECLARE
  filehandle  utl_file.file_type; --句柄
  begin
  filehandle := utl_file.fopen('TESTFILE','hello.txt','w'); --打开文件
  utl_file.put_line(filehandle,'Hello Oracle!');--写入一行记录
  utl_file.put_line(filehandle,'Hello World!');
  utl_file.put_line(filehandle,'你好,胖子!');
  utl_file.fclose(filehandle);--关闭句柄
  end;
  /

备注:

fopen有一个参数max_linesize,下面是原文解释

Maximum number of characters for each line, including the newline character, for this file (minimum value 1, maximum value 32767). If unspecified, Oracle supplies a default value of 1024.

 

3.读取操作

--测试读取
set serveroutput on;

 DECLARE
  filehandle  utl_file.file_type;
  filebuffer varchar2(500);
  begin
  filehandle := utl_file.fopen('TESTFILE','hello.txt','R');
  IF utl_file.is_open(filehandle) THEN
    dbms_output.put_line('file is open!');
  END IF;
  loop
  begin
  utl_file.get_line(filehandle,filebuffer);
  dbms_output.put_line(filebuffer);
  EXCEPTION
   WHEN no_data_found THEN
    exit ;
   WHEN OTHERS  THEN
    dbms_output.put_line('EXCEPTION1:'||SUBSTR(SQLERRM, 1, 100)) ; 
  end;
  end loop;
  utl_file.fclose(filehandle);
    IF utl_file.is_open(filehandle)  THEN
      dbms_output.put_line('file is open!');
    else
    dbms_output.put_line('file is close!');
  END IF;
  utl_file.fcopy('TESTFILE', 'hello.txt', 'TESTFILE', 'hello.dat');--复制
  utl_file.fcopy('TESTFILE', 'hello.txt', 'TESTFILE', 'hello2.dat');
  utl_file.fcopy('TESTFILE', 'hello.txt', 'TESTFILE', 'hello.xls');

  utl_file.frename('TESTFILE','hello.xls','TESTFILE','frenamehello.xls',TRUE);--重命名
  utl_file.fremove('TESTFILE', 'hello2.dat');--删除文件
  EXCEPTION
     WHEN OTHERS  THEN
    dbms_output.put_line('EXCEPTION2:'||SUBSTR(SQLERRM, 1, 100)) ; 
  end;
  /

4.判断文件是否存在(读,重命名,复制,删除都要判断文件是否存在)

--判断文件是否存在
DECLARE
ex    BOOLEAN;--文件是否存在

flen NUMBER;--文件长度? 这个地方不知道怎么理 (原文 file_length The length of the file in bytes. NULL if file does not exist.)
bsize NUMBER;--文件大小
BEGIN
utl_file.fgetattr('TESTFILE', 'hello.txt', ex, flen, bsize);
IF ex THEN
    dbms_output.put_line('File Exists');
ELSE
    dbms_output.put_line('File Does Not Exist');
END IF;
dbms_output.put_line('File Length: ' || TO_CHAR(flen));
dbms_output.put_line('Block Size: ' || TO_CHAR(bsize));
END fgetattr;
/

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-无-为-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值