oracle中utl_file包讀寫檔案操作執行個體學習

來源:互聯網
上載者:User

標籤:

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

 

oracle中utl_file包讀寫檔案操作執行個體學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.