您的位置:首页 > 数据库 > Oracle

oracle读写文件--利用utl_file包对磁盘文件的读写操作

2008-08-27 15:10 531 查看

摘要:

用户提出一个需求,即ORACLE中的一个表存储了照片信息,字段类型为BLOB,要求能导出成文件形式. 本想写个C#程序来做,后来想起ORACLE有很多包,功能很好很强大,于是网上参考了些文章完成了. 主要是用了ORACLE的两个包:UTL_FILE和DBMS_LOB.

实现过程:

第一步:以管理员用户登陆设置可操作目录

--CREATE DIRECTORY privilege is granted only to SYS and SYSTEM by default.

create or replace directory BLOBDIR as 'D:\PIC';

grant read,write on directory BLOBDIR to sharedb;

GRANT EXECUTE ON utl_file TO sharedb;

select * from ALL_DIRECTORIES;

第二步:普通用户登陆,编写存储过程

create or replace procedure test_error

(

str out varchar2,

str2 out varchar2

)

as

begin

declare

isto_file utl_file.file_type;

err_num number;

i number;

k number;

m number;

err_msg varchar2(100);

fp_buffer varchar2(4000);

begin

isto_file := utl_file.fopen('IST0_DIR', 'kj021320.txt', 'W');

i:=0;

while (i<2)

loop

utl_file.put_line(isto_file, 'My');

i:=i+1;

end loop;

utl_file.fflush(isto_file);

utl_file.fclose(isto_file);

isto_file := utl_file.fopen('IST0_DIR', 'kj021320.txt', 'a');

m:=0;

while (m<2)

loop

utl_file.put_line(isto_file, 'My');

m:=m+1;

end loop;

utl_file.fflush(isto_file);

utl_file.fclose(isto_file);

isto_file := utl_file.fopen('IST0_DIR', 'kj021320.txt', 'R');

str2:='';

loop

utl_file.get_line (isto_file , fp_buffer );

str2:=str2 || fp_buffer;

end loop;

utl_file.fclose(isto_file);

loop

k:=11;

end loop;

EXCEPTION

WHEN OTHERS THEN

str:=substr(sqlerrm,1,100);

end;

end test_error;

PROCEDURE prc_utl_file

IS

file_read_handle utl_file.file_type;

file_write_handle utl_file.file_type;

is_opened BOOLEAN;

v_one_line VARCHAR2(1000);

b_file_exist BOOLEAN;

n_file_length NUMBER(10,2);

bi_block_size BINARY_INTEGER;

BEGIN

--1.读/读写模式打开文件

file_read_handle := utl_file.fopen('TEST_UTL_FILE_DIR_READ', 'orcl_ora_396.trc', 'R');

file_write_handle := utl_file.fopen('TEST_UTL_FILE_DIR_WRITE', 'TEST_UTL_FILE_DIR_WRITE.txt', 'W');

--2.检查文件是否打开

is_opened := utl_file.is_open(file_read_handle);

IF is_opened THEN

dbms_output.put_line('file is opened');

ELSE

dbms_output.put_line('file is not opened');

END IF;

--3.读文件

LOOP

BEGIN

utl_file.get_line(file_read_handle, v_one_line);

dbms_output.put_line(v_one_line);

-- 4.将读入结果写入新文件中

utl_file.put(file_write_handle, v_one_line);

utl_file.new_line(file_write_handle, 2);

--utl_file.put_line(file_write_handle, v_one_line);

--utl_file.put_line(file_write_handle, v_one_line, TRUE);

EXCEPTION

WHEN no_data_found THEN

EXIT;

WHEN OTHERS THEN

dbms_output.put_line('error1:'||SQLERRM);

EXIT;

END;

END LOOP;

--5.关闭文件

utl_file.fclose(file_read_handle);

--6确认所有未决的数据都写到物理文件中

--utl_file.fflush(file_write_handle);

utl_file.fclose(file_write_handle);

--utl_file.fclose_all;

--6.检查文件是否关闭

is_opened := utl_file.is_open(file_read_handle);

IF is_opened THEN

dbms_output.put_line('file is still opened');

ELSE

dbms_output.put_line('file is already closed');

END IF;

--7.拷贝文件

utl_file.fcopy('TEST_UTL_FILE_DIR_WRITE', 'TEST_UTL_FILE_DIR_WRITE.txt', 'TEST_UTL_FILE_DIR_WRITE', 'TEST_UTL_FILE_DIR_WRITE_COPY.txt', 1, 10);

--8.删除文件

utl_file.fcopy('TEST_UTL_FILE_DIR_WRITE', 'TEST_UTL_FILE_DIR_WRITE.txt', 'TEST_UTL_FILE_DIR_WRITE', 'TEST_UTL_FILE_DIR_WRITE_COPY_DELETE.txt', 1, 10);

utl_file.fremove('TEST_UTL_FILE_DIR_WRITE', 'TEST_UTL_FILE_DIR_WRITE_COPY_DELETE.txt');

--9.重命名

--utl_file.frename('TEST_UTL_FILE_DIR_WRITE', 'TEST_UTL_FILE_DIR_WRITE_COPY.txt', 'TEST_UTL_FILE_DIR_WRITE', 'TEST_UTL_FILE_DIR_WRITE_COPY_DELETE_RENAME.txt', FALSE);

--10.获取重命名后的文件属性

utl_file.fgetattr('TEST_UTL_FILE_DIR_WRITE', 'TEST_UTL_FILE_DIR_WRITE_COPY.txt',b_file_exist,n_file_length, bi_block_size);

IF b_file_exist THEN

dbms_output.put_line('n_file_length:'||n_file_length||'\n'||'bi_block_size'||bi_block_size);

END IF;

END;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: