UTL_FILE
- 提供了在存储过程或函数中对系统文件进行读写的功能集合。
函数名 |
参数类型 |
结果类型 |
描述 |
fopen |
Location text,filename text, open_mode text |
utl_file.file_type |
打开一个文件用于输入或输出 |
is_open |
file utl_file. file_type |
bolean |
检查一个文件句柄是否指向一个打开的文件 |
get_line |
file utl_file.file_type,OUT buffer text,len integer |
text |
从一个打开的文件中读取指定的一行文本 |
get_nextline |
file utl_file.file_type,OUT buffer text |
text |
获得下一行 |
put |
file utl_file.file_type,buffer anyelement/buffer text |
boolean |
将一个字符串写入到文件 |
put_line |
file utl_file.file_type,buffer text,autoflush boolean |
boolean |
在文件中写入一行,在行的末尾写入一个行终结符 |
new_line |
file utl_file.file_type,lines integer |
boolean |
在文件末尾写入一个行终结符 |
putf |
file utl_file.file_type,format text,arg1 text,arg2 text |
boolean |
格式化输出过程 |
fflush |
file utl_file.file_type |
null |
将文件缓存刷到物理写入 |
fclose |
file utl_file.file_type |
utl_file.file_type |
关闭一个文件 |
fclose_all |
null |
null |
关闭所有打开的文件句柄 |
fremove |
location text,filename text |
null |
删除磁盘上的文件 |
frename |
location text,filename text,dest_dir text,dest_file text,overwrite boolean |
null |
重命名一个已经存在的文件 |
fcopy |
src_location text,src_filename text,dest_location text,dest_filename text,start_line integer,end_line integer |
null |
将一个文件中的内容拷贝到另一个文件中 |
fgetattr |
Location text,filename text,OUT fexists boolean,OUT file_length gigint,OUT blocksize integer |
record |
获取文件属性 |
tmpdir |
null |
text |
获得临时目录路径 |
shell执行:
cat > /tmp/test.txt
111aaa
222bbb
333ccc
444ddd
555eee
666fff
777ggg
888hhh
999iii
101010jjj
111111kkk
数据库下执行:
insert into utl_file.utl_file_dir values ('/tmp');
select utl_file.fcopy('/tmp', 'test.txt', '/tmp', 'test_cp.txt',3,6);
fclose/is_open关闭打开的文件:
do language plpgsql $$
declare
vInHandle utl_file.file_type;
vOutHandle utl_file.file_type;
begin
vInHandle := utl_file.fopen('/tmp', 'test.txt', 'R');
vOutHandle := utl_file.fopen('/tmp', 'test_cp.txt', 'W');
raise notice 'OPEN';
IF utl_file.is_open(vInHandle) THEN
utl_file.fclose(vInHandle);
END IF;
IF not utl_file.is_open(vInHandle) THEN
raise notice 'close';
END IF;
end;
$$;
fgetattr查看文件的属性:
do language plpgsql $$
declare
ex boolean;
flen int;
bsize int;
begin
-- utl_file.fgetattr('/tmp'::text, 'test.txt'::text, ex, flen, bsize);
select * from utl_file.fgetattr('/tmp'::text, 'test.txt'::text) into ex,flen,bsize;
IF ex THEN
raise notice 'File Exists';
ELSE
raise notice 'File Does Not Exist' ;
END IF;
raise notice 'FileLength: %' , flen;
raise notice 'BlockSize: %',bsize;
end;
$$;
put_line/fopen/fflush/get_line/fclose_all读写文件:
do language plpgsql $$
declare
vInHandle utl_file.file_type;
vOutHandle utl_file.file_type;
vNewLine VARCHAR2(250);
begin
vInHandle := utl_file.fopen('/tmp', 'test.txt','R');
LOOP
BEGIN
select * from utl_file.get_line(vInHandle)into vNewLine ;
raise notice '%', vNewLine;
EXCEPTION
WHEN OTHERS THEN
EXIT;
END;
END LOOP;
vOutHandle := utl_file.fopen('/tmp', 'test_cp.txt', 'w');
perform utl_file.put_line(vOutHandle, 'hello lsxy');
utl_file.fflush(vOutHandle);
utl_file.fclose_all();
end;
$$;
new_line/PUTF格式化输出过程:
do language plpgsql $$
declare
vInHandle utl_file.file_type;
begin
vInHandle := utl_file.fopen('/tmp', 'test.txt', 'w');
perform utl_file.new_line(vInHandle, 2);
utl_file.fclose(vInHandle);
end;
$$;
do language plpgsql $$
declare
my_world varchar2(4) := 'Zork';
vInHandle utl_file.file_type;
begin
vInHandle := utl_file.fopen('/tmp', 'test.txt', 'w');
perform utl_file.PUTF(vInHandle, 'Hello, world! I come from %s with.\n',my_world);
perform utl_file.PUT_line(vInHandle, 'Hello, world!');
vInHandle := utl_file.fclose(vInHandle);
end;
$$;
get_nextline获取下一行信息:
do language plpgsql $$
declare
vInHandle utl_file.file_type;
vOutHandle utl_file.file_type;
vNewLine VARCHAR2(250);
begin
vInHandle := utl_file.fopen('/tmp', 'test.txt','R');
select * from utl_file.get_nextline(vInHandle)into vNewLine ;
raise notice '%', vNewLine;
end;
$$;
put将一个字符写入到文件:
do language plpgsql $$
declare
vOutHandle utl_file.file_type;
vNewLine VARCHAR2(250);
begin
vOutHandle := utl_file.fopen('/tmp', 'test_cp.txt', 'w');
perform utl_file.put(vOutHandle, 'a');
utl_file.fflush(vOutHandle);
utl_file.fclose(vOutHandle);
end;
$$;
tmpdir获取临时目录路径:
select utl_file.tmpdir();
frename文件重命名:
do language plpgsql $$
declare
begin
perform utl_file.frename('/tmp','test.txt','/tmp','test1.txt',TRUE);
end;
$$;
fremove文件删除:
do language plpgsql $$
declare
begin
utl_file.fremove('/tmp', 'test_cp.txt');
end;
$$;