UTL_FILE - read and write file or Random Access of Files

automatically generate the loader control file:

--create the directory
CREATE OR REPLACE DIRECTORY UTL_DIR AS 'c:/oraload';

--write the loader control file
CREATE OR REPLACE PROCEDURE Write_File(text_context VARCHAR2,filename VARCHAR2)
IS
file_handle utl_file.file_type;
Write_content VARCHAR2(32767);
Write_file_name VARCHAR2(1000);
BEGIN
--open file
write_file_name := filename;
file_handle := utl_file.fopen('UTL_DIR',write_file_name,'w');
write_content := text_context;

--write file
IF utl_file.is_open(file_handle) THEN
utl_file.put_line(file_handle,write_content);
dbms_output.put_line(write_content);
END IF;

--close file
utl_file.fclose(file_handle);
EXCEPTION
WHEN OTHERS THEN
BEGIN
IF utl_file.is_open(file_handle) THEN
utl_file.fclose(file_handle);
END IF;
END;
END Write_File;
/


--generate the control file of table specified
CREATE OR REPLACE PROCEDURE generate_ldrFile(tablename IN VARCHAR2) AS
   l_curr_line       LONG;
   l_table_name      user_tables.table_name%TYPE;
BEGIN
   select   table_name
   into     l_table_name
   from     user_tables
   where    table_name =upper(tablename);
   l_curr_line := '
LOAD DATA
INFILE '''||lower(l_table_name)||'.txt'||''' '||'
Truncate INTO TABLE ';
  l_curr_line := l_curr_line||l_table_name||'
  FIELDS TERMINATED BY '||''''||','||''''||'
  TRAILING NULLCOLS
(';
   for rec in  ( select  table_name,column_name,column_id,nullable
   from    user_tab_columns
   where   table_name =upper(tablename)
   order by  column_id) loop
   if rec.column_id = 1 THEN
         IF rec.nullable = 'Y' THEN
      l_curr_line := l_curr_line||'
      '||rpad(rec.column_name||' NULLIF '||rec.column_name||'='||'''''',80);
         ELSE
      l_curr_line := l_curr_line||'
      '||rpad(rec.column_name,80);
         END IF;
    else
       IF rec.nullable = 'Y' THEN
      l_curr_line := l_curr_line||'
      ,'||rpad(rec.column_name||' NULLIF '||rec.column_name||'='||'''''',80);
       ELSE
      l_curr_line := l_curr_line||'
      ,'||rpad(rec.column_name,80);
       END IF;
  end if;
   end loop;
  l_curr_line := l_curr_line||')';
  Write_File(l_curr_line,l_table_name||'.ctl');
END generate_ldrFile;
/

--generate all control files
CREATE OR REPLACE PROCEDURE generate_All_ldrFile AS
BEGIN
  FOR x IN (SELECT table_name FROM user_tables) LOOP
      generate_ldrFile(x.table_name);
  END LOOP;
END generate_All_ldrFile;
/

--exec PROCEDURE
execute generate_All_ldrFile
exit;
//////////////////////////////////////////////////////////////////////////////////////////////

UTL_FILE - Random Access of Files

I was recently asked if I could read the first and last line of a file using PL/SQL. Until recently this was not possible without reading the whole file or using a Java stored procedure, but Oracle9i Release 2 supports random access of files through the UTL_FILE package. This article shows a simple mechanism to solve this problem using these UTL_FILE enhancements.

First we create a directory object pointing to the location of the file of interest:

CREATE OR REPLACE DIRECTORY my_docs AS '/usr/users/oracle/';

Prior to Oracle9i Release 2 I would solve this problem by reading the whole file as follows:

SET SERVEROUTPUT ON SIZE 1000000
DECLARE
  l_file         UTL_FILE.file_type;
  l_location     VARCHAR2(100) := 'MY_DOCS';
  l_filename     VARCHAR2(100) := 'temp';
  l_text         VARCHAR2(32767);
BEGIN
  -- Open file.
  l_file := UTL_FILE.fopen(l_location, l_filename, 'r', 32767);
  
  -- Read and output first line.
  UTL_FILE.get_line(l_file, l_text, 32767);
  DBMS_OUTPUT.put_line('First Line: |' || l_text || '|');

  -- Read through the file until we reach the last line.
  BEGIN
    LOOP
      UTL_FILE.get_line(l_file, l_text, 32767);
    END LOOP;
  EXCEPTION
    WHEN NO_DATA_FOUND THEN
      NULL;
  END;
  
  -- Output the last line.
  DBMS_OUTPUT.put_line('Last Line : |' || l_text || '|');

  -- Close the file.
  UTL_FILE.fclose(l_file);
END;
/

Using the UTL_FILE enhancements we can now do the following:

SET SERVEROUTPUT ON SIZE 1000000
DECLARE
  l_file         UTL_FILE.file_type;
  l_location     VARCHAR2(100) := 'MY_DOCS';
  l_filename     VARCHAR2(100) := 'temp';
  l_exists       BOOLEAN;
  l_file_length  NUMBER;
  l_blocksize    NUMBER;
  l_text         VARCHAR2(32767);
BEGIN
  UTL_FILE.fgetattr(l_location, l_filename, l_exists, l_file_length, l_blocksize);

  IF l_exists THEN
    -- Open file.
    l_file := UTL_FILE.fopen(l_location, l_filename, 'r', 32767);
    
    -- Read and output first line.
    UTL_FILE.get_line(l_file, l_text, 32767);
    DBMS_OUTPUT.put_line('First Line: |' || l_text || '|');
    UTL_FILE.FSEEK (l_file, l_file_length-1);
  
    -- Step backwards through the file until we reach the start of the last line.
    FOR i IN REVERSE 0 .. l_file_length-2 LOOP
      UTL_FILE.FSEEK (l_file, NULL, -2);
      UTL_FILE.get_line(l_file, l_text, 1);
      EXIT WHEN l_text IS NULL;
    END LOOP;
    
    -- Read and output the last line.
    UTL_FILE.get_line(l_file, l_text, 32767);
    DBMS_OUTPUT.put_line('Last Line : |' || l_text || '|');
  
    -- Close the file.
    UTL_FILE.fclose(l_file);
  END IF;
END;
/

The fgetattr procedure allows us to check that the file exists and return the file length. We then read the first line using the get_line procedure as normal. To get the last line we need to skip to the end of the file using the fseek procedure and work backwards until we hit a line terminator. The get_line procedure does not return line terminators so we detect it's presence by checking for the return of an empty line. We can then display the last line.

I'm not too sure about the performance of the fseek procedure. For large files it's often quicker to read the whole file which is a bit disappointing. Even so, if you need to move both backwards and forwards in the file, these enhancements may still be useful.

reference link:

http://www.psoug.org/reference/utl_file.html

内容概要:本文详细探讨了基于MATLAB/SIMULINK的多载波无线通信系统仿真及性能分析,重点研究了以OFDM为代表的多载波技术。文章首先介绍了OFDM的基本原理和系统组成,随后通过仿真平台分析了不同调制方式的抗干扰性能、信道估计算法对系统性能的影响以及同步技术的实现与分析。文中提供了详细的MATLAB代码实现,涵盖OFDM系统的基本仿真、信道估计算法比较、同步算法实现和不同调制方式的性能比较。此外,还讨论了信道特征、OFDM关键技术、信道估计、同步技术和系统级仿真架构,并提出了未来的改进方向,如深度学习增强、混合波形设计和硬件加速方案。; 适合人群:具备无线通信基础知识,尤其是对OFDM技术有一定了解的研究人员和技术人员;从事无线通信系统设计与开发的工程师;高校通信工程专业的高年级本科生和研究生。; 使用场景及目标:①理解OFDM系统的工作原理及其在多径信道环境下的性能表现;②掌握MATLAB/SIMULINK在无线通信系统仿真中的应用;③评估不同调制方式、信道估计算法和同步算法的优劣;④为实际OFDM系统的设计和优化提供理论依据和技术支持。; 其他说明:本文不仅提供了详细的理论分析,还附带了大量的MATLAB代码示例,便于读者动手实践。建议读者在学习过程中结合代码进行调试和实验,以加深对OFDM技术的理解。此外,文中还涉及了一些最新的研究方向和技术趋势,如AI增强和毫米波通信,为读者提供了更广阔的视野。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值