Set ServerOutput On
declare
  v_line             varchar2(32767);

  c_location         constant varchar2(80) := 'UTL_FILE_TEST';
  c_source_filename  constant varchar2(80) := 'test.txt';
  c_copy_filename    constant varchar2(80) := 'copy.txt';
  c_ren_filename     constant varchar2(80) := 'ren.txt';


  procedure Show_Attr ( p_filename in varchar2 ) is
    v_exists         boolean;
    v_file_length    number;
    v_block_size     binary_integer;
  begin
    Utl_File.Fgetattr (

      location    => c_location,
      filename    => p_filename,
      fexists     => v_exists,
      file_length => v_file_length,
      block_size  => v_block_size );
    -- Bug #2240685. v_exists is always returned TRUE
    -- But non-existent file has ZERO file_length and block_size

    if not v_exists then Raise_Application_Error ( -20000, 'Bug #2240685 is fixed' ); end if;
    Dbms_Output.Put_Line (
      p_filename             || ' : ' ||
      To_Char(v_file_length) || ' : ' ||
      To_Char(v_block_size ) );
  end Show_Attr;

  procedure Remove ( p_filename in varchar2 ) is

  begin
    Utl_File.Fremove (
      location    => c_location,
      filename    => p_filename );
  exception
    when Utl_File.Delete_Failed then
      Utl_File.Fclose_All;
      Raise_Application_Error ( -20000, 'Fremove: Delete_Failed trapped' );
    when Utl_File.Invalid_Operation then

      Utl_File.Fclose_All;
      Raise_Application_Error ( -20000, 'Fremove: Invalid_Operation trapped' );
  end Remove;

  procedure Full_Copy ( p_source in varchar2, p_dest in varchar2 ) is
  begin
    Utl_File.Fcopy (
      src_location  => c_location,
      src_filename  => p_source,
      dest_location => c_location,

      dest_filename => p_dest );
--    start_line       binary_integer in default
-- end_line binary_integer in default

exception when Utl_File.Invalid_Operation then Utl_File.Fclose_All; Raise_Application_Error ( -20000, 'Fcopy: Invalid_Operation trapped' ); end Full_Copy; procedure Rename ( p_source in varchar2, p_dest in varchar2 ) is begin Utl_File.Frename ( src_location => c_location, src_filename => p_source, dest_location => c_location, dest_filename => p_dest, overwrite => false); exception when Utl_File.Rename_Failed then Utl_File.Fclose_All; Raise_Application_Error ( -20000, 'Frename: Rename_Failed trapped' ); end Rename; begin Show_Attr ( c_source_filename ); Show_Attr ( c_copy_filename ); Full_Copy ( c_source_filename, c_copy_filename ); --Full_Copy ( 'not_there.txt', c_copy_filename ) /* Uncomment to see Utl_File.Invalid_Operation */; Show_Attr ( c_copy_filename ); -- Create ren.txt before running this block to see Utl_File.Rename_Failed Rename ( c_copy_filename, c_ren_filename ); Show_Attr ( c_ren_filename ); Remove ( c_ren_filename ); -- Create protected.txt by hand and do "chmod a-x protected.txt" --Remove ( 'protected.txt' ) /* Uncomment to see Utl_File.Invalid_Operation */; end; /