- createorreplacepackageYLFileop_packis
- --Author:YANLEI
- --Created:2008-12-2416:08:02
- --Purpose:文件操作包
- YL_DIRCONSTANTvarchar2(32):='YL_DIR';--财务表收费FormID
- --从后面查找
- functionlastindex(strinvarchar2,findstrinvarchar2)returninteger;
- --获得一个文件的路径
- functiongetpath(filenameinvarchar2)returnvarchar2;
- --获得一个文件的文件
- functiongetfilename(filenameinvarchar2)returnvarchar2;
- procedurewritefile(filenameinvarchar2,strinlong);
- functionreadfilereturnvarchar2;
- endYLFileop_pack;
- /
- createorreplacepackagebodyYLFileop_packis
- isto_fileutl_file.file_type;--如上
- proceduresetoraclepath(pathinvarchar2)is
- sqlstrvarchar2(1024);
- begin
- sqlstr:='createorreplacedirectory'||YL_DIR||'as'''||path||'''';
- executeimmediatesqlstr;
- end;
- --oracle字符串从后面查找byyanleigisEmail:landgis@126.com
- functionlastindex(strinvarchar2,findstrinvarchar2)returnintegeris
- iinteger;
- numinteger;
- sublninteger;
- begin
- num:=length(str);
- subln:=length(findstr);
- ifsubln>numthen
- return-1;
- endif;
- i:=num-subln+1;
- whilei>0loop
- ifsubstr(str,i,subln)=findstrthen
- returni;
- endif;
- i:=I-1;
- endloop;
- return-1;
- end;
- --获得一个文件的路径
- functiongetpath(filenameinvarchar2)returnvarchar2is
- idxinteger;
- begin
- idx:=lastindex(filename,'/');
- if(idx>0)then
- returnsubstr(filename,1,idx);
- endif;
- return'';
- end;
- --获得一个文件的文件
- functiongetfilename(filenameinvarchar2)returnvarchar2is
- idxinteger;
- begin
- idx:=lastindex(filename,'/');
- if(idx>0)then
- returnsubstr(filename,idx+1);
- endif;
- return'';
- end;
- functionopenfile(filenameinvarchar2,stateinvarchar2)returnintegeris
- pathvarchar2(1024);
- begin
- path:=getpath(filename);
- setoraclepath(path);
- isto_file:=utl_file.fopen(YL_DIR,getfilename(filename),state);
- return1;
- exception
- whenothersthen
- dbms_output.put_line(sqlcode||':'||sqlerrm);
- return0;
- end;
- functionopenfileread(filenameinvarchar2)returnintegeris
- begin
- returnopenfile(filename,'R');
- end;
- functionopenfilewrite(filenameinvarchar2)returnintegeris
- begin
- returnopenfile(filename,'W');
- end;
- procedurewriteline(strinvarchar2)is
- begin
- utl_file.put_line(isto_file,str);--写入字符串
- end;
- procedurewritelinelong(strinlong)is
- begin
- utl_file.put_line(isto_file,str);--写入字符串
- end;
- procedureclosefileis
- sqlstrvarchar2(1024);
- begin
- --utl_file.fflush(isto_file);--刷缓冲
- utl_file.fclose(isto_file);--关闭文件指针
- sqlstr:='dropdirectory'||YL_DIR;
- executeimmediatesqlstr;
- end;
- procedurewritefile(filenameinvarchar2,strinlong)is
- begin
- ifopenfilewrite(filename)>0then
- writelinelong(str);
- closefile();
- endif;
- end;
- functionreadlinereturnvarchar2is
- --读取一行放到fp_buffer变量里面
- fp_buffervarchar2(20000);--
- strvarchar2(4000);
- begin
- loop
- begin
- utl_file.get_line(isto_file,str);--读取一行放到fp_buffer变量里面
- fp_buffer:=fp_buffer||str;
- fp_buffer:=fp_buffer||chr(13);--chr(10)
- exception
- whenno_data_foundthen
- exit;
- end;
- endloop;
- returnfp_buffer;
- end;
- functionreadfilereturnvarchar2is
- strvarchar2(20000);
- begin
- ifopenfileread('c:/yl.w')>0then
- str:=readline;
- closefile();
- returnstr;
- endif;
- return'读文件失败';
- end;
- endYLFileop_pack;
- /
oracle 文件操作封装
最新推荐文章于 2025-08-09 18:20:04 发布