oracle 文件操作封装

  1. createorreplacepackageYLFileop_packis
  2. --Author:YANLEI
  3. --Created:2008-12-2416:08:02
  4. --Purpose:文件操作包
  5. YL_DIRCONSTANTvarchar2(32):='YL_DIR';--财务表收费FormID
  6. --从后面查找
  7. functionlastindex(strinvarchar2,findstrinvarchar2)returninteger;
  8. --获得一个文件的路径
  9. functiongetpath(filenameinvarchar2)returnvarchar2;
  10. --获得一个文件的文件
  11. functiongetfilename(filenameinvarchar2)returnvarchar2;
  12. procedurewritefile(filenameinvarchar2,strinlong);
  13. functionreadfilereturnvarchar2;
  14. endYLFileop_pack;
  15. /
  16. createorreplacepackagebodyYLFileop_packis
  17. isto_fileutl_file.file_type;--如上
  18. proceduresetoraclepath(pathinvarchar2)is
  19. sqlstrvarchar2(1024);
  20. begin
  21. sqlstr:='createorreplacedirectory'||YL_DIR||'as'''||path||'''';
  22. executeimmediatesqlstr;
  23. end;
  24. --oracle字符串从后面查找byyanleigisEmail:landgis@126.com
  25. functionlastindex(strinvarchar2,findstrinvarchar2)returnintegeris
  26. iinteger;
  27. numinteger;
  28. sublninteger;
  29. begin
  30. num:=length(str);
  31. subln:=length(findstr);
  32. ifsubln>numthen
  33. return-1;
  34. endif;
  35. i:=num-subln+1;
  36. whilei>0loop
  37. ifsubstr(str,i,subln)=findstrthen
  38. returni;
  39. endif;
  40. i:=I-1;
  41. endloop;
  42. return-1;
  43. end;
  44. --获得一个文件的路径
  45. functiongetpath(filenameinvarchar2)returnvarchar2is
  46. idxinteger;
  47. begin
  48. idx:=lastindex(filename,'/');
  49. if(idx>0)then
  50. returnsubstr(filename,1,idx);
  51. endif;
  52. return'';
  53. end;
  54. --获得一个文件的文件
  55. functiongetfilename(filenameinvarchar2)returnvarchar2is
  56. idxinteger;
  57. begin
  58. idx:=lastindex(filename,'/');
  59. if(idx>0)then
  60. returnsubstr(filename,idx+1);
  61. endif;
  62. return'';
  63. end;
  64. functionopenfile(filenameinvarchar2,stateinvarchar2)returnintegeris
  65. pathvarchar2(1024);
  66. begin
  67. path:=getpath(filename);
  68. setoraclepath(path);
  69. isto_file:=utl_file.fopen(YL_DIR,getfilename(filename),state);
  70. return1;
  71. exception
  72. whenothersthen
  73. dbms_output.put_line(sqlcode||':'||sqlerrm);
  74. return0;
  75. end;
  76. functionopenfileread(filenameinvarchar2)returnintegeris
  77. begin
  78. returnopenfile(filename,'R');
  79. end;
  80. functionopenfilewrite(filenameinvarchar2)returnintegeris
  81. begin
  82. returnopenfile(filename,'W');
  83. end;
  84. procedurewriteline(strinvarchar2)is
  85. begin
  86. utl_file.put_line(isto_file,str);--写入字符串
  87. end;
  88. procedurewritelinelong(strinlong)is
  89. begin
  90. utl_file.put_line(isto_file,str);--写入字符串
  91. end;
  92. procedureclosefileis
  93. sqlstrvarchar2(1024);
  94. begin
  95. --utl_file.fflush(isto_file);--刷缓冲
  96. utl_file.fclose(isto_file);--关闭文件指针
  97. sqlstr:='dropdirectory'||YL_DIR;
  98. executeimmediatesqlstr;
  99. end;
  100. procedurewritefile(filenameinvarchar2,strinlong)is
  101. begin
  102. ifopenfilewrite(filename)>0then
  103. writelinelong(str);
  104. closefile();
  105. endif;
  106. end;
  107. functionreadlinereturnvarchar2is
  108. --读取一行放到fp_buffer变量里面
  109. fp_buffervarchar2(20000);--
  110. strvarchar2(4000);
  111. begin
  112. loop
  113. begin
  114. utl_file.get_line(isto_file,str);--读取一行放到fp_buffer变量里面
  115. fp_buffer:=fp_buffer||str;
  116. fp_buffer:=fp_buffer||chr(13);--chr(10)
  117. exception
  118. whenno_data_foundthen
  119. exit;
  120. end;
  121. endloop;
  122. returnfp_buffer;
  123. end;
  124. functionreadfilereturnvarchar2is
  125. strvarchar2(20000);
  126. begin
  127. ifopenfileread('c:/yl.w')>0then
  128. str:=readline;
  129. closefile();
  130. returnstr;
  131. endif;
  132. return'读文件失败';
  133. end;
  134. endYLFileop_pack;
  135. /

你可能感兴趣的:(oracle)