utl file包進行OS檔案操作

2021-04-13 14:23:42 字數 3707 閱讀 3343

利用

oracle utl_file 1

:建立檔案目錄:

首先在資料庫伺服器上建立相應的檔案目錄。 1

.1方法:

在初始化檔案配置檔案

init.ora

的配置中將

utl_file_dir = 『e

:/temp

』指定路徑;

1.2

方法:

建立路徑物件:

create directory test_dir as e:temp

建議用第二種方法; 2

:開啟和關閉檔案:

所有的檔案控制代碼都擁有

utl_file.file_type

。file_type

在utl_file

規範中進行了定義。

2.1:fopen

(location in varchar2, filename in varchar2, open_mode in varchar2

)return file_type;

location

是路徑引數,

filename

是檔名,

open_mode

是開啟模式;

有效值:『

r』是讀文字,『

w』是寫文字,『

a』是附加文字,引數不分大小寫,如果指定『

a』但是檔案不存在,它會用『

w』先建立出來,『

w』有覆蓋的功能;

2.2 : fclose(file_handle in out file_type);

唯一的引數是檔案控制代碼,就是關閉檔案;

fclose_all

關閉所有檔案控制代碼;

2.3: is_open(file_handle in file_type) return boolean;

判斷檔案是否開啟; 3

:檔案輸出:

所有的函式如下:

3.1:put(file_handle in file_type, buffer in varchar2);

檔案輸出,但是不會在檔案中附加新行字元(

newline

),必須用

put_line

或者new_line

向檔案中輸入終結符;

3.2:new_line

(file_handle in file_type, lines in natural := 1);

向檔案中寫入乙個或者多個行終結符;

3.3:put_line(file_handle in file_type, buffer in varchar2); 等價

put後接著用

new_line;

3.4:

putf(file_handle in file_type, format in varchar2,

arg1 in varchar2 default null,

arg2 in varchar2 default null,

arg3 in varchar2 default null,

arg4 in varchar2 default null,

arg5 in varchar2 default null) 和

put類似,但是它允許輸出字串是帶格式的,格式字元中

n是換行,

%s被可選引數取代;

例如:declare

v_outputfile utl_file.file_type;

v_name varchar2(20) := 『scott』;

begin

v_outputfile := utl_file.fopen(..);

utl_file.putf(v_outputfile,

『hi there! n my name is %s,and i am a %s major.n』,

v_name,

『computer science』 );

fclose(v_outputfile);

end;

輸出檔案格式:

hi there!

my name is scott ,and i am a computer science major.

3.5:

輸出檔案應用例項:

create or replace procedure p_mmr_new

(p_start_time out date,

p_end_time out date)

isv_file utl_file.file_type;

v_string varchar2(100);

v_error exception;

v_i number;

cursor cur_ms_no is

select distinct substr(trim(a.ms_no),1,11)

from msno.t_upload_msno a,

msno.t_msno_black b

where substr(a.ms_no,1,11) = b.ms_no(+)

and b.ms_no is null;

v_count number;

v_ms_no varchar2(20);

begin

p_start_time:=sysdate;

v_count:=0;

-- insert

v_file := utl_file.fopen('outer_dir','ivr170.txt', 'w',32767);

open cur_ms_no ;

loop

fetch cur_ms_no into v_ms_no;

exit when cur_ms_no%notfound;

utl_file.put_line(v_file, v_ms_no);

v_count:=v_count+1;

if v_count>=5000 then

utl_file.fflush(v_file);

--utl_file.fclose(v_file);

--v_file := utl_file.fopen('outer_dir','v_ms_no.txt', 'a',32767);

v_count:=0;

end if;

end loop;

utl_file.fclose(v_file);

close cur_ms_no;

--close

p_end_time:=sysdate;

exception

when v_error then

utl_file.fclose(v_file);

return;

end;

4:檔案輸入:

4.1 get_line(file_handle in file_type, buffer out varchar2);

從檔案中讀出資料;

示例:declare

v_newline varchar2(200);

begin

v_filehandle := utl_file.fopen(p_filedir, p_filename, 『r』);

utl_file.get_line(vfilehandle, v_newline);

insert into t1 (tip) values(v_newline);

end;

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

oracle中utl file包讀寫檔案操作學習 在oracle中utl file包提供了一些操作文字檔案的函式和過程,學習了一下他的基本操作 www.2cto.com 1.建立directory,並給使用者授權 建立directory create or replace directory tes...

利用UTL FILE包實現檔案I O操作

檔案i o對於資料庫的開發來說顯得很重要,比如如果資料庫中的一部分資料來自於磁碟檔案,那麼就需要使用i o介面把資料匯入到資料庫中來。在pl sql中沒有直接的i o介面,一般在除錯程式時可以使用oracle自帶的dbms output包的put line函式 即向螢幕進行i o操作 即可,但是對於...

NSFileHandle對檔案進行讀寫操作

nslog 路徑 fullpath nsfilehandle filehandle nsfilehandle filehandleforwritingatpath fullpath filehandle seektoendoffile filehandle writedata log datausi...