oracle中utl_file包讀寫文件操作實(shí)例學(xué)習(xí)
更新時間:2013年03月12日 16:46:49 作者:
在oracle中utl_file包提供了一些操作文本文件的函數(shù)和過程,接下來和大家一起學(xué)習(xí)他的基本操作,感興趣的你可以參考下哈希望可以幫助到你
在oracle中utl_file包提供了一些操作文本文件的函數(shù)和過程,學(xué)習(xí)了一下他的基本操作
1.創(chuàng)建directory,并給用戶授權(quán)
--創(chuàng)建directory
create or replace directory TESTFILE as '/home/oracle/zxx/test';
--給用戶授權(quán)
grant read, write on directory TESTFILE to zxx;
詳細(xì)介紹
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);--關(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.讀取操作
--測試讀取
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ù)制,刪除都要判斷文件是否存在)
--判斷文件是否存在
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.創(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;
詳細(xì)介紹
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/u_file.htm
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;
相關(guān)文章
oracle統(tǒng)計(jì)時間段內(nèi)每一天的數(shù)據(jù)(推薦)
這篇文章主要介紹了oracle統(tǒng)計(jì)時間段內(nèi)每一天的數(shù)據(jù),需要的朋友可以參考下2018-03-03
Oracle(90)數(shù)據(jù)庫如何創(chuàng)建用戶(User)
這篇文章主要介紹了在Oracle數(shù)據(jù)庫中創(chuàng)建用戶的過程,包括連接到數(shù)據(jù)庫、創(chuàng)建用戶、分配權(quán)限、分配表空間和設(shè)置賬戶狀態(tài),提供了詳細(xì)的步驟和代碼示例,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-12-12
SQL案例學(xué)習(xí)之字符串的合并與拆分方法總結(jié)
這篇文章主要給大家介紹了關(guān)于SQL案例學(xué)習(xí)之字符串的合并與拆分的相關(guān)資料,文中分別介紹了兩種方法,對大家學(xué)習(xí)或者使用oracle具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-08-08
Oracle數(shù)據(jù)庫把多行轉(zhuǎn)一列逗號分割兩種方法
Oracle將行轉(zhuǎn)換為列是指將關(guān)系型數(shù)據(jù)庫中的行數(shù)據(jù)轉(zhuǎn)換為列數(shù)據(jù)的操作,這篇文章主要給大家介紹了關(guān)于Oracle數(shù)據(jù)庫把多行轉(zhuǎn)一列逗號分割兩種方法的相關(guān)資料,需要的朋友可以參考下2024-07-07
深入淺析Oracle數(shù)據(jù)庫管理之創(chuàng)建和刪除數(shù)據(jù)庫
本篇文章給大家介紹oracle數(shù)據(jù)庫管理之創(chuàng)建和刪除數(shù)據(jù)庫,本文從數(shù)據(jù)庫管理概述、數(shù)據(jù)庫管理方法、數(shù)據(jù)庫的準(zhǔn)則、使用dbca創(chuàng)建數(shù)據(jù)庫、使用dbca刪除數(shù)據(jù)庫等五大方面展開話題,需要的朋友一起學(xué)習(xí)吧2015-10-10
Linux服務(wù)器下oracle實(shí)現(xiàn)rman自動備份的方式
為確保oracle數(shù)據(jù)庫數(shù)據(jù)的安全和一致性,一般我們都需要利用備份手段進(jìn)行數(shù)據(jù)庫的備份,在oracle數(shù)據(jù)庫中,rman因其強(qiáng)大的功能和完善的手段,成為數(shù)據(jù)庫備份的首選,故以下通過兩種方式配置腳本,實(shí)現(xiàn)rman自動備份,需要的朋友可以參考下2024-11-11

