---
title: "利用Oracle的UTL_FILE包来实现对磁盘文件的I/O操作"
author: "helloJ"
date: "2008-08-25T20:12:12Z"
tags: []
slug: "oracle-utl-file-i-o"
draft: false
lastmod: "2008-08-25T20:12:12Z"
id: 232541
url: "https://helloaq.iteye.com/blog/232541"
---

摘要：本文主要讨论如何利用Oracle的UTL\_FILE包来实现对磁盘文件的I/O操作。   
    
  　　文件I/O对于数据库的开发来说显得很重要，比如如果数据库中的一部分数据来自于磁盘文件，   
          那么就需要使用I/O接口把数据导入到数据库中来。在   PL/SQL中没有直接的I/O接口，   
          一般在调试程序时可以使用Oracle自带的DBMS\_OUTPUT包的put\_line函数（即向屏幕进行I/O   操作）即可，   
        但是对于磁盘文件的I/O操作它就无能为力了。其实Oracle同样也提供了可以进行文件I/O的实用包-----UTL\_FILE包，   
        利用这个实用包提供的函数来实现对磁盘的I/O操作。   
    
  　　1．   准备工作   
    
  　　由于Oracle数据库对包创建的目录有一个安全管理的问题，所以并不是所有的文件目录能够被UTL\_FILE包所访问，   
          要更新这种目录设置，就得到init.ora里将UTL\_FILE\_DIR域设置为\*，这样UTL\_FILE包就可以对所有的目录文件进行访问了。   
    
  　　2．   文件I/O的实施   
    
  　　UTL\_FILE包提供了很多实用的函数来进行I/O操作，主要有以下几个函数：   
    
  　　fopen   
    
  　　打开指定的目录路径的文件。   
    
  　　get\_line   
    
  　　获取指定文件的一行的文本。   
    
  　　put\_line   
    
  　　向指定的文件写入一行文本。   
    
  　　fclose   
    
  　　关闭指定的文件。   
    
  　　下面利用这些函数，实现从文件取数据，然后将数据写入到相应的数据库中。   
    
  create   or   replace   procedure   loadfiledata(p\_path   varchar2,p\_filename   varchar2)   as   
  v\_filehandle   utl\_file.file\_type;   --定义一个文件句柄   
  v\_text   varchar2(100);   --存放文本   
  v\_name   test\_loadfile.name%type;   
  v\_addr\_jd   test\_loadfile.addr\_jd%type;   
  v\_region   test\_loadfile.region%type;   
  v\_firstlocation   number;   
  v\_secondlocation   number;   
  v\_totalinserted   number;   
  begin   
  if   (p\_path   is   null   or   p\_filename   is   null)   then   
  goto   to\_end;   
  end   if;   
  v\_totalinserted:=0;   
  /\*open   specified   file\*/   
  v\_filehandle:=utl\_file.fopen(p\_path,p\_filename,'r');   
  loop   
  begin   
  utl\_file.get\_line(v\_filehandle,v\_text);   
  exception   
  when   no\_data\_found   then   
  exit;   
  end   ;   
  v\_firstlocation:=instr(v\_text,',',1,1);   
  v\_secondlocation:=instr(v\_text,',',1,2);   
  v\_name:=substr(v\_text,1,v\_firstlocation-1);   
  v\_addr\_jd:=substr(v\_text,v\_firstlocation+1,v\_secondlocation-v\_firstlocation-1);   
  v\_region:=substr(v\_text,v\_secondlocation+1);   
  /\*插入数据库操作\*/   
  insert   into   test\_loadfile   
  values   (v\_name,v\_addr\_jd,v\_region);   
  commit;   
  end   loop;   
  &lt;&lt;to\_end&gt;&gt;   
  null;   
  end   loadfiledata;   
    
    
    
  可以不用在init.ora中改的   
  只要用管理员的权限登陆，执行：   
  create   directory   UTL\_FILE\_TEST   as   '\\\*'     
  应该就可以了,不用新启动oracle的   
  不过其他用户要使用此目录要授权的   
  /   
    
    
  grant   create   any   directory   to   scott;   
    grant   create   any   library   to   scott;   
    create   or   replace   directory   utllobdir   as   'C:\\ep';   
    
    
  在initsid.ora文件中，加入或修改   
      设置utl\_file\_dir的要点：   
    1。   utl\_file\_dir=*     这表示你能操作任何目录，尽量不要用   
    2。   utl\_file\_dir=d:\\   这表示你能操作d:\\目录下的文件，但你不能操作d:\\目录下的子目录   
    3。注意在设置   
      utl\_file\_dir=路径时，如果路径是长路径名，例如c:\\my   temp目录，则你必须加上''，例如：   
      utl\_file\_dir='c:\\my   temp'   
    4。utl\_file\_dir可以是多个路径   
          utl\_file\_dir=c:\\,d:\\,d:\\temp,'c:\\my   temp'   
    5。设置完必须重新启动数据库

 

================================================

 

命令设置：

[system@oracle9i](mailto:system@oracle9i) as sysdba

 

user:system

pwd:\*\*\*\*\**

数据库：oracle9i

连接为：sysdba

执行命令：

alter system set utl\_file\_dir='D:\\exportDataTest' scope=spfile;  
grant execute on utl\_file to scott;

 

服务器关闭-重启

(

（1） 以oracle身份登录数据库，命令：su – oracle  
（2） 进入Sqlplus控制台，命令：sqlplus /nolog  
（3） 以系统管理员登录，命令：connect / as sysdba

（4） 启动数据库，命令：startup  
（5） 如果是关闭数据库，命令：shutdown immediate  
（6） 退出sqlplus控制台，命令：exit  
（7） 进入监听器控制台，命令：lsnrctl  
（8） 启动监听器，命令：start  
（9） 退出监听器控制台，命令：exit  
（10） 重启数据库结束

)

登陆scott，就可以用utl\_file包对路径'D:\\exportDataTest' 进行文件的输入输出操作