zoukankan      html  css  js  c++  java
  • 使用UTL_FILE包来读写文件

    使用UTL_FILE包来读写文件

    1、创建DIRCTORY
    SQL> conn sys/sys@iceman as sysdba;
    已连接。
    SQL> create or replace directory utl_test as 'E:\utl';
    目录已创建。
    SQL> grant read,write on directory utl_test to iceman;
    授权成功。

    2、利用UTL_FILE包来写入数据到文件test.txt,此文件可以先创建,也可以不用。
    SQL> conn iceman/iceman@iceman;
    已连接。
    SQL> declare
    2  fhandle utl_file.file_type;
    3  begin
    4  fhandle := utl_file.fopen('UTL_TEST','test.txt','w');
    5  utl_file.put_line(fhandle,'the first write');
    6  utl_file.put_line(fhandle,'the second write');
    7  utl_file.fclose(fhandle);
    8  end;
    9  /
    PL/SQL 过程已成功完成。
    注意:第四行中,fhandle := utl_file.fopen('UTL_TEST','test.txt','w')中的directory名一定要大写,不然会报下面的错误:
    declare
    *
    ERROR 位于第 1 行:
    ORA-29280: 目录路径无效
    ORA-06512: 在"SYS.UTL_FILE", line 18
    ORA-06512: 在"SYS.UTL_FILE", line 424
    ORA-06512: 在line 4

    3、利用UTL_FILE包来读取文件中的内容
    SQL> set serveroutput on
    SQL>  Declare
    2  fhandle utl_file.file_type;
    3  fp_buffer Varchar2(1000);
    4  Begin
    5  fhandle :=utl_file.fopen('UTL_TEST','test.txt','R');
    6  utl_file.get_line(fhandle,fp_buffer);
    7  dbms_output.put_line(fp_buffer);
    8  utl_file.get_line(fhandle,fp_buffer);
    9  dbms_output.put_line(fp_buffer);
    10  utl_file.fclose(fhandle);
    11  End;
    12  /
    the first write
    the second write

    PL/SQL 过程已成功完成。

  • 相关阅读:
    String类
    数学类
    同一场景下多个图层之间的调用
    茶壶在触摸机制下旋转的三种方式
    犀牛3D模型下载
    纹理--高清设计素材下载
    cocos2d-x-2.x与3.x帧动画实现方式的改变
    toast提示框的实现
    MenuItem创建注意事项
    Cocos2d-x 面试题解 整理01
  • 原文地址:https://www.cnblogs.com/sinoJay/p/2955359.html
Copyright © 2011-2022 走看看