Set ServerOutput On
declare

  v_line           varchar2(32767);
  c_location       constant varchar2(80) := 'UTL_FILE_TEST'
                                            /* new in 9.2.0 - use a DIRECTORY */;
  c_filename       constant varchar2(80) := 'test.txt';
  v_handle         Utl_File.File_Type;


  procedure Put_Line ( p_buffer in varchar2 ) is begin
    Utl_File.Put_Line (
      file     => v_handle,
      buffer   => p_buffer,
      autoflush => true ) /* repeat with "autoflush => false" and notice the difference */;

  end Put_Line;

begin
  v_handle := Utl_File.Fopen (
    location    => c_location,
    filename    => c_filename,
    open_mode   => 'w',

    max_linesize => 32767 );

  Utl_File.Put (
      file     => v_handle,
      buffer   => '1st line. Using Put' );

  Utl_File.New_Line (
    file     => v_handle,

    lines    => 3 );

  Utl_File.Putf (
file => v_handle,
format => 'Error in %s due to %s.\nCall %s',
arg1 => 'something',
arg2 => 'something else',
arg3 => 'somebody' )
;
Utl_File.New_Line ( file => v_handle ); Utl_File.Fflush ( file => v_handle ) /* Comment out to see that Put, New_Line don't autoflush */; Wait.Go /* observe test.txt with "tail -f" and execute Wait.Finish from another session */; for j in 3..10 loop Put_Line ( To_Char(j) || 'th line. It''s not too long' ); Wait.Go; end loop; Utl_File.Fclose ( file => v_handle ); v_handle := Utl_File.Fopen ( location => c_location, filename => c_filename, open_mode => 'r', max_linesize => 32767 ); begin loop -- when len is smaller than the current line, the next Get_Line carries on -- where the previous one left off in the same line Utl_File.Get_Line (
file => v_handle,
buffer => v_line );
-- len => 11 /* uncomment to see the effect */ );
Dbms_Output.Put_Line ( v_line || '--' ); end loop; exception when no_data_found then null; end; Utl_File.Fclose ( file => v_handle ); end; /