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 过程已成功完成。

  • 相关阅读:
    字符读取流缓冲区
    Runtime、System类
    字符流和字节流
    将C盘一个文本文件复制到D盘。
    DateDemo
    Collection单列集合 Map双列集合
    泛型
    Collection接口
    String类
    Python代码约定
  • 原文地址:https://www.cnblogs.com/sinoJay/p/2955359.html
Copyright © 2011-2022 走看看