Search This Blog

Monday, August 22, 2022

Read file content from an Oracle Directory in PL/SQL/ in IFS

Read single file

DECLARE
   file_handle_      UTL_FILE.FILE_TYPE;
   directory_name_   CONSTANT all_directories.directory_name%TYPE := 'DIRECTORY_NAME';
   file_name_        VARCHAR2(4000);
   line_             VARCHAR2(4000);
   file_content_     CLOB;
BEGIN
   -- initialize file content
   file_name_    := 'log_main.txt';
   file_content_ := NULL;
         
   BEGIN
      -- open file in read mode
      file_handle_ := UTL_FILE.FOPEN(directory_name_, file_name_, 'R');
            
      -- fetch data from file
      LOOP
         UTL_File.Get_Line(file_handle_, line_);
         
         file_content_ := file_content_ || line_;
      END LOOP;
            
      -- close the file
      UTL_FILE.FCLOSE(file_handle_);
   EXCEPTION
      WHEN no_data_found THEN
         -- close the file
         UTL_FILE.FCLOSE(file_handle_);
      WHEN OTHERS THEN
         -- close the file
         UTL_FILE.FCLOSE(file_handle_);
         
         -- reset file content
         file_content_ := NULL;
   END;
            
   -- output file content
   Dbms_Output.Put_Line(file_content_);
END;

Read multiple files NOTE: Filenames got via IFS Intface_Server_File_API.File_List procedure

DECLARE
   file_handle_      UTL_FILE.FILE_TYPE;
   directory_name_   CONSTANT all_directories.directory_name%TYPE := 'DIRECTORY_NAME';
   file_ist_         Intface_File_Types := Intface_File_Types();
   line_             VARCHAR2(4000);
   file_content_     CLOB;
BEGIN
   file_ist_ := Intface_Server_File_API.File_List(directory_name_);
   
   FOR i IN 1..file_ist_.count LOOP
      -- initialize file content
      file_content_ := NULL;
      
      BEGIN
         Dbms_Output.Put_Line(file_ist_(i).file_name);
         Dbms_Output.Put_Line('-----------------------------------------------------------------');
         
         -- open file in read mode
         file_handle_ := UTL_FILE.FOPEN(directory_name_, file_ist_(i).file_name, 'R');
            
         -- fetch data from file
         LOOP
            UTL_File.Get_Line(file_handle_, line_);
               
            file_content_ := file_content_ || line_;
         END LOOP;
            
         -- close the file
         UTL_FILE.FCLOSE(file_handle_);
      EXCEPTION
         WHEN no_data_found THEN
            -- close the file
            UTL_FILE.FCLOSE(file_handle_);
         WHEN OTHERS THEN
            -- close the file
            UTL_FILE.FCLOSE(file_handle_);
               
            -- reset file content
            file_content_ := NULL;
      END;
            
      -- output file content
      Dbms_Output.Put_Line(file_content_);
      Dbms_Output.Put_Line('');
   END LOOP;
END;

Related Links:

> Get file names in Oracle Directory in IFS without using Complied Java Source
> Get file names in an Oracle Directory using Java in Oracle PL/SQL

Read file content from an Oracle Directory in PL/SQL/ in IFS

Read single file DECLARE file_handle_ UTL_FILE.FILE_TYPE; directory_name_ CONSTANT all_directories.directory_name%TYPE := '...

Popular Posts