Oracle UTL_FILE 用法例子

来源:互联网 发布:铅酸电池老化原因知乎 编辑:程序博客网 时间:2024/05/17 23:04

 

General Information Note: O/S permissions are those of the user 'Oracle' ... not the schema owner or connected userSource{ORACLE_HOME}/rdbms/admin/utlfile.sqlFirst Availability7.3.4init.ora Parametersutl_file_dirutl_file_dir=c:/oraload
utl_file_dir=c:/temp
utl_file_dir=*
Open Modes AAppend TextABAppend Byte ModeRRead TextRBRead Byte ModeWWrite TextWBWrite Byte ModeFCLOSEClose A File Opened By UTL_FILEutl_file.fclose(<file_handle>)see FOPEN demo FCLOSE_ALLClose All Files Opened By UTL_FILEutl_file.fclose_all;set serveroutput on

DECLARE
vInHandle utl_file.file_type;
vOutHandle utl_file.file_type;
BEGIN
vInHandle := utl_file.fopen('ORALOAD', 'test.txt', 'R');
vOutHandle := utl_file.fopen('ORALOAD', 'out.txt', 'W');

IF utl_file.is_open(vInHandle) THEN
    utl_file.fclose_all;
    dbms_output.put_line('Closed All');
END IF;
END fopen;
/
FCOPYCopies a contiguous portion of a file to a newly created fileutl_file.fcopy (
location   IN VARCHAR2,
filename   IN VARCHAR2,
dest_dir   IN VARCHAR2,
dest_file IN VARCHAR2,
start_line IN PLS_INTEGER DEFAULT 1,
end_line   IN PLS_INTEGER DEFAULT NULL);
-- demo requires creating directory CTEMP ... see link at bottom of page

BEGIN
utl_file.fcopy('ORALOAD', 'dump.txt', 'ORALOAD', 'didit.txt');
END;
/
FFLUSHPhysically writes pending data to the file identified by the file handleutl_file.fflush (<file_handle>);See Write demo FGETATTRReads and returns the attributes of a disk fileutl_file.fgetattr(
location    IN VARCHAR2,
filename    IN VARCHAR2,
exists      OUT BOOLEAN,
file_length OUT NUMBER,
blocksize   OUT NUMBER);
set serveroutput on

DECLARE
ex    BOOLEAN;
flen NUMBER;
bsize NUMBER;
BEGIN
utl_file.fgetattr('ORALOAD', 'test.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;
/
FGETPOS Returns the current relative offset position within a file, in bytesutl_file.fgetpos(fileid IN file_type) RETURN PLS_INTEGER;See Read-Write demo FOPENOpen A File For Read Operationsutl_file.fopen(
<file_location IN VARCHAR2>,
<file_name     IN VARCHAR2>,
<open_mode     IN VARCHAR2>,
<max_linesize IN BINARY_INTEGER>)
RETURN <file_type_package_data_type;
DECLARE
vInHandle utl_file.file_type;
vNewLine VARCHAR2(250);
BEGIN
vInHandle := utl_file.fopen('ORALOAD', 'test.txt', 'R');
LOOP
    BEGIN
      utl_file.get_line(vInHandle, vNewLine);
      dbms_output.put_line(vNewLine);
    EXCEPTION
      WHEN OTHERS THEN
        EXIT;
    END;
END LOOP;
utl_file.fclose(vInHandle);
END fopen;
/
Open A File For Write Operations<file_handle> := utl_file.fopen(<file_location, file_name, 'w') FOPEN_NCHAR Open a file to read or write a text file in Unicode instead of in the database charset FREMOVEDelete An Operating System Fileutl_file.fremove (location IN VARCHAR2, filename IN VARCHAR2);BEGIN
utl_file.fremove('ORALOAD', 'dump.txt');
END fremove;
/
FRENAMERename An Operating System Fileutl_file.frename (
location IN VARCHAR2,
filename IN VARCHAR2,
dest_dir IN VARCHAR2,
dest_file IN VARCHAR2,
overwrite IN BOOLEAN DEFAULT FALSE);
BEGIN
utl_file.frename('ORALOAD','test.txt','ORALOAD','x.txt',TRUE);
END frename;
/
FSEEK Adjusts the file pointer forward or backward within the file by the number of bytes specifiedutl_file.fseek (
fid             IN utl_file.file_type,
absolute_offset IN PL_INTEGER DEFAULT NULL,
relative_offset IN PLS_INTEGER DEFAULT NULL);
See Read-Write demo GETLINERead a Line from a fileutl_file.getline (
file     IN FILE_TYPE,
buffer   OUT VARCHAR2,
linesize IN NUMBER,
len      IN PLS_INTEGER DEFAULT NULL);
See Read demos GETLINE_NCHAR Same as GETLINE except can be used to read Unicode rather than the database's character set GET_RAW Reads a RAW string value from a file and adjusts the file pointer ahead by the number of bytes readutl_file.get_raw (
fid IN utl_file.file_type,
r   OUT NOCOPY RAW,
len IN PLS_INTEGER DEFAULT NULL);
See UTL_MAIL demo IS_OPENReturns True If A File Handle Is Open: Otherwise Falseutl_file.is_open (file IN FILE_TYPE) RETURN BOOLEAN;See FCLOSE_ALL Demo NEW_LINE Writes one or more operating system-specific line terminators to a fileutl_file.NEW_LINE (file IN FILE_TYPE, lines IN NATURAL := 1);See Read Demo PUTWrites a string to a fileutl_file.put(
file   IN FILE_TYPE,
buffer IN VARCHAR2);
See Write demo PUTFA PUT procedure with formattingutl_file.putf(
file   IN FILE_TYPE,
format IN VARCHAR2,
[arg1 IN VARCHAR2 DEFAULT NULL,
. . .
arg5   IN VARCHAR2 DEFAULT NULL]);
See Write demo PUT_LINE Writes a line to a file. Appends an operating system-specific line terminatorutl_file.put_line(
file      IN FILE_TYPE,
buffer    IN VARCHAR2,
autoflush IN BOOLEAN DEFAULT FALSE);
See Read-Write demo PUT_NCHARWrites a Unicode string to a file PUT_RAWAccepts as input a RAW data value and writes the value to the output bufferutl_file.PUT_RAW (
fid       IN utl_file.file_type,
r         IN RAW,
autoflush IN BOOLEAN DEFAULT FALSE);
See extract_blob Demo PUT_LINE_NCHARWrites a Unicode line to a file PUTF_NCHARWrites a Unicode string to a file

 

本文转自:http://www.psoug.org/reference/utl_file.html