utl_file包可以用来读写操作系统上的文本文件,utl_file提供了在客户端(form等等)和服务器端的文件访问功能。
utl_file包可以用来读写操作系统上的文本文件,utl_file提供了在客户端(form等等)和服务器端的文件访问功能。
创建测试目录:
新建一个command window;
创建目录:(以system用户登录数据库)
sql> create or replace directory cux_log_dir as '/home/appltest/debug';
directory created
赋权限。
sql> grant read, write on directory cux_log_dir to public;
grant succeeded
检查目录是否成功创建
select * from all_directories dir where dir.directory_name = 'cux_log_dir';
过程和函数:
fopen语法:
utl_file.fopen (location in varchar2,
filename in varchar2,
open_mode in varchar2,
max_linesize in binary_integer default 1024)return file_type;
filename略。
open_mode指明文件打开的模式。有如下几种:
max_linesize指定文件文本每一行存放的最大字符数。
fclose功能:关闭一个打开的文件。
fclose_all
结果为:
结果为:
fcopy语法:
utl_file.fcopy ( src_locationin varchar2,
src_filenamein varchar2,
dest_locationin varchar2,
dest_filename in varchar2,
start_linein binary_integer default 1,
end_linein binary_integer default null);
src_filename将要被复制的来源文件
dest_location 被创建的目标文件存放的目录名。
dest_filename 从来源文件创建的目标文件。
测试程序之前:
测试代码:
测试程序之后:
fflush语法:
utl_file.fflush (file in file_type);
fgetattrfilename in varchar2,
fexists out boolean,
file_length out number,
block_size out binary_integer);
block_size文件系统块的字节大小。
测试:
declare
l_loc all_directories.directory_name%type := 'cux_log_dir';
l_file utl_file.file_type;
l_file_exsits boolean;
l_file_length number;
l_block_size binary_integer;
l_buffer varchar2(1024);
begin
utl_file.fgetattr(location => l_loc,
filename => 'l001.log',
fexists => l_file_exsits,
file_length => l_file_length,
block_size => l_block_size);
if l_file_exsits then
l_file := utl_file.fopen(location => l_loc,
filename => 'l001.log',
open_mode => 'r');
dbms_output.put_line('file exsits');
dbms_output.put_line('file length:' || l_file_length);
dbms_output.put_line('block sieze :' || l_block_size);
end if;
utl_file.fclose_all;
end;
,