oracle中utl_file包读写文件操作实例学习
网络编程 2021-07-05 16:08www.168986.cn编程入门
在oracle中utl_file包提供了一些操作文本文件的函数和过程,接下来和大家一起学习他的基本操作,感兴趣的你可以参考下哈希望可以帮助到你
在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./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,狼蚁网站SEO优化是原文解释
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;
1.创建directory,并给用户授权
代码如下:
--创建directory
create or replace directory TESTFILE as '/home/oracle/zxx/test';
--给用户授权
grant read, write on directory TESTFILE to zxx;
详细介绍
http://download.oracle./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,狼蚁网站SEO优化是原文解释
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;
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指