Write text to a file with UTL_FILE package : File Write with UTL_FILE « System Packages « Oracle PL/SQL Tutorial






SQL>
SQL> DECLARE
  2     fid UTL_FILE.FILE_TYPE;
  3     f VARCHAR(200);
  4     v VARCHAR2(32767);
  5     topdir VARCHAR2(100);
  6
  7     PROCEDURE recNgo (str IN VARCHAR2)
  8     IS
  9     BEGIN
 10        DBMS_OUTPUT.PUT_LINE ('UTL_FILE error ' || str || ' in ' || f);
 11        UTL_FILE.FCLOSE (fid);
 12     END;
 13
 14     PROCEDURE writeit
 15     IS
 16     BEGIN
 17         f := 'c:\a';
 18         fid := UTL_FILE.FOPEN (f, 'temp.txt', 'W');
 19         UTL_FILE.PUT_LINE (fid, v);
 20         UTL_FILE.FCLOSE (fid);
 21        DBMS_OUTPUT.PUT_LINE ('Successful write to ' || f);
 22      EXCEPTION
 23         WHEN UTL_FILE.INVALID_PATH THEN recNgo ('invalid_path');
 24         WHEN UTL_FILE.INVALID_MODE THEN recNgo ('invalid_mode');
 25         WHEN UTL_FILE.INVALID_FILEHANDLE THEN recNgo ('invalid_filehandle');
 26         WHEN UTL_FILE.INVALID_OPERATION THEN recNgo ('invalid_operation');
 27         WHEN UTL_FILE.READ_ERROR THEN recNgo ('read_error');
 28         WHEN UTL_FILE.WRITE_ERROR THEN recNgo ('write_error');
 29         WHEN UTL_FILE.INTERNAL_ERROR THEN recNgo ('internal_error');
 30     END;
 31  BEGIN
 32     writeit;
 33  END;
 34  /



SQL>








31.35.File Write with UTL_FILE
31.35.1.Open the output file for writing
31.35.2.Save string to a file with UTL_FILE.PUTF
31.35.3.Save lines to a file
31.35.4.Write text to a file with UTL_FILE package