SET SERVEROUTPUT ON
DECLARE
v_handle UTL_FILE.FILE_TYPE;
v_dirname VARCHAR2(250);
v_filename VARCHAR2(250);
v_output VARCHAR2(250);
v_getmsg VARCHAR2(250);
v_errmsg VARCHAR2(1000);
v_opcheck BOOLEAN;
BEGIN
v_dirname := '/home/oracle';
v_filename := 'sample.txt';
v_output := 'HELLO WORLD!';
v_handle := UTL_FILE.FOPEN(v_dirname,
v_filename,
'w',
256);
UTL_FILE.PUT_LINE(v_handle, v_output);
UTL_FILE.FFLUSH(v_handle);
UTL_FILE.PUT(v_handle, v_output);
UTL_FILE.FCLOSE(v_handle);
v_handle := UTL_FILE.FOPEN(v_dirname,
v_filename,
'r',
256);
UTL_FILE.GET_LINE(v_handle, v_getmsg);
DBMS_OUTPUT.PUT_LINE(
'GET_MESSAGE : ' || v_getmsg);
UTL_FILE.FCLOSE_ALL;
v_opcheck := UTL_FILE.IS_OPEN(v_handle);
DBMS_OUTPUT.PUT_LINE(CASE
WHEN v_opcheck IS NULL THEN 'UNKNOWN'
WHEN v_opcheck THEN 'TRUE'
WHEN NOT v_opcheck THEN 'FALSE'
END);
BEGIN
UTL_FILE.PUT_LINE(v_handle, v_output);
EXCEPTION
WHEN UTL_FILE.INVALID_FILEHANDLE THEN
v_errmsg := SQLERRM;
DBMS_OUTPUT.PUT_LINE(v_errmsg);
END;
EXCEPTION WHEN OTHERS THEN
UTL_FILE.FCLOSE_ALL;
v_errmsg := SQLERRM;
DBMS_OUTPUT.PUT_LINE(v_errmsg);
END;
/
|
DO $$
DECLARE
v_handle UTL_FILE.FILE_TYPE;
v_dirname VARCHAR2(250);
v_filename VARCHAR2(250);
v_output VARCHAR2(250);
v_getmsg VARCHAR2(250);
v_errmsg VARCHAR2(1000);
v_opcheck BOOLEAN;
BEGIN
PERFORM DBMS_OUTPUT.SERVEROUTPUT(TRUE);
PERFORM DBMS_OUTPUT.ENABLE(NULL);
v_dirname := '/home/pgsql';
v_filename := 'sample.txt';
v_output := 'HELLO WORLD!';
v_handle := UTL_FILE.FOPEN(v_dirname,
v_filename,
'w',
256);
PERFORM UTL_FILE.PUT_LINE(v_handle, v_output);
PERFORM UTL_FILE.PUT(v_handle, v_output);
PERFORM UTL_FILE.FFLUSH(v_handle);
PERFORM UTL_FILE.NEW_LINE(v_handle, 1);
v_handle := UTL_FILE.FCLOSE(v_handle);
v_handle := UTL_FILE.FOPEN(v_dirname,
v_filename,
'r',
256);
v_getmsg := UTL_FILE.GET_LINE(v_handle);
PERFORM DBMS_OUTPUT.PUT_LINE(
'GET_MESSAGE : ' || v_getmsg);
PERFORM UTL_FILE.FCLOSE_ALL();
v_opcheck := UTL_FILE.IS_OPEN(v_handle);
PERFORM DBMS_OUTPUT.PUT_LINE(CASE
WHEN v_opcheck IS NULL THEN 'UNKNOWN'
WHEN v_opcheck THEN 'TRUE'
WHEN NOT v_opcheck THEN 'FALSE'
END);
BEGIN
PERFORM UTL_FILE.PUT_LINE(v_handle, v_output);
EXCEPTION
WHEN RAISE_EXCEPTION THEN
v_errmsg := SQLERRM;
PERFORM DBMS_OUTPUT.PUT_LINE(v_errmsg);
END;
EXCEPTION WHEN OTHERS THEN
PERFORM UTL_FILE.FCLOSE_ALL();
v_errmsg := SQLERRM;
PERFORM DBMS_OUTPUT.PUT_LINE(v_errmsg);
END;
$$
;
|