国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > Oracle > 正文

oracle中utl_file包讀寫文件操作實例學習

2024-08-29 13:55:19
字體:
供稿:網(wǎng)友
在oracle中utl_file包提供了一些操作文本文件的函數(shù)和過程,學習了一下他的基本操作
1.創(chuàng)建directory,并給用戶授權(quán)

復(fù)制代碼 代碼如下:


--創(chuàng)建directory
create or replace directory TESTFILE as '/home/oracle/zxx/test';
--給用戶授權(quán)
grant read, write on directory TESTFILE to zxx;


詳細介紹

2.寫入操作

復(fù)制代碼 代碼如下:


---測試寫入
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);--關(guān)閉句柄
end;


備注:
fopen有一個參數(shù)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.讀取操作

復(fù)制代碼 代碼如下:


--測試讀取
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');--復(fù)制
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.判斷文件是否存在(讀,重命名,復(fù)制,刪除都要判斷文件是否存在)

復(fù)制代碼 代碼如下:


--判斷文件是否存在
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;

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿瓦提县| 三江| 桃园县| 乌恰县| 广昌县| 白山市| 临泉县| 柏乡县| 汶上县| 绥江县| 青冈县| 得荣县| 安龙县| 兰州市| 城市| 明星| 高清| 巩义市| 耒阳市| 杨浦区| 灯塔市| 额敏县| 溆浦县| 新平| 平陆县| 罗江县| 大荔县| 鱼台县| 大埔县| 澎湖县| 汾西县| 静乐县| 贡山| 百色市| 犍为县| 兴城市| 团风县| 无极县| 江门市| 林周县| 六盘水市|