directory及utl_file的例子:
create or replace directory UTF_DIR_01 as '/u01/test';
grant read,write on directory UTF_DIR_01 to wang ;
eg 1: 将某些信息写入文件:
declare
v_file utl_file.file_type;
begin
v_file := utl_file.fopen('UTF_DIR_01', 'test_dir.txt', 'w');
utl_file.put_line(v_file , '1. write test msg to utl_file');
utl_file.put_line(v_file , '2. write test msg to utl_file');
utl_file.fclose(v_file);
end;
/
fopen 打开指定的目录路径的文件。
get_line 获取指定文件的一行的文本。
put_line 向指定的文件写入一行文本。
fclose 关闭指定的文件。
eg 2: 读取文件所有信息,写入表中。
declare
v1 VARCHAR2(32767);
v_file utl_file.file_type;
begin
v_file := utl_file.fopen('UTF_DIR_01', 'test_dir.txt', 'r');
loop
utl_file.get_line(v_file , v1);
insert into wang.test_dir values(v1);
commit;
-- dbms_output.put_line('v1: ' || v1);
end loop ;
utl_file.fclose(v_file);
exception
when NO_DATA_FOUND then
utl_file.fclose(v_file);
end;
/