oracle中utl file包讀寫檔案操作學習

2021-12-30 02:32:33 字數 2907 閱讀 7535

oracle中utl_file包讀寫檔案操作學習

在oracle中utl_file包提供了一些操作文字檔案的函式和過程,學習了一下他的基本操作  www.2cto.com  

1.建立directory,並給使用者授權

--建立directory

create or replace directory testfile as '/home/oracle/zxx/test';

--給使用者授權  www.2cto.com  

grant read, write on directory testfile to zxx;

(英文) 詳細介紹:

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;/ 

oracle利用utl file包來讀寫檔案

oracle利用使用utl file包 create or replace procedure loadfiledata p path varchar2,p filename varchar2 is v filehandle utl file.file type 定義乙個檔案控制代碼 v text ...

utl file包的應用

第一步 以管理員使用者登陸 如 conn sys password sid as sysdba 第二步 設定可操作目錄 需要指定utl file包可以操作的目錄。在oracle 10g以前,可以用以下方法 1 alter system set utl file dir e utl scope spf...

oracle的檔案(UTL FILE)操作

oracle提供了乙個能否對作業系統操作的工具包utl file 想要oracle對檔案進行操作就要先建立乙個directory來指向作業系統目錄下的具體某個目錄 create directory report dir as home oracle chenlong report dir 為建立di...