• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >Mysql > oracle中utl_file包读写文件操作实例学习

oracle中utl_file包读写文件操作实例学习

作者:匿名 字体:[增加 减小] 来源:互联网 时间:2018-12-05

匿名通过本文主要向大家介绍了oracle,utl_file,读写文件等相关知识,希望本文的分享对您有所帮助

在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.com/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,下面是原文解释
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;
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 浅析drop user与delete from mysql.user的区别
  • mysql prompt的用法详解
  • 如何使用索引提高查询速度
  • 深入mysql创建自定义函数与存储过程的详解
  • JDBC数据库的使用操作总结
  • MySQL查询优化:LIMIT 1避免全表扫描提高查询效率
  • MySQL与Oracle的语法区别详细对比
  • 有关mysql中ROW_COUNT()的小例子
  • MySQL 百万级分页优化(Mysql千万级快速分页)
  • 从创建数据库到存储过程与用户自定义函数的小感

相关文章

  • 2018-12-05mysql数据库如何导出数据?
  • 2018-12-05分享网站群发站内信数据库表设计
  • 2017-05-11mysql生成随机字符串函数分享
  • 2018-12-05谈谈常见写法的实例讲解
  • 2018-12-05Mysql一些复杂的sql语句(查询与删除重复的行)
  • 2018-12-05mysql中的添,删,改,查
  • 2018-12-05union组合结果集时的order问题
  • 2018-12-05 Couchbase Server 2.0 发布,NoSQL 数据库
  • 2018-12-05mysql处理添加外键时提示error 150 问题的解决方法
  • 2017-05-11mysql5.0入侵测试以及防范方法分享

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • SQL Server 数据库备份和还原认识和总结 (一)
    • MySQL数据库操作的基本命令实例详解
    • MySQL数据类型和存储机制详解
    • MySQL常用时间函数详解(推荐)
    • PHP运算符与操作符入门详解
    • linux mysql 报错:MYSQL:The server quit without updating PID
    • SQLServer 参数化查询经验分享
    • SQL Server中的T-SQL的基本对象
    • MySQL如何创建用户?
    • Mysql启动中 InnoDB: Error: log file ./ib_logfile0 is of diff

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有