Oracle file operation Encapsulation

Source: Internet
Author: User
  1. Create or replace package ylfileop_pack is
  2. -- Author: yanlei
  3. -- Created: 16:08:02
  4. -- Purpose: File Operation package
  5. Yl_dir constant varchar2 (32): = 'yl _ dir'; -- Financial table charge formid
  6. -- Search from the end
  7. Function lastindex (STR in varchar2, findstr in varchar2) return integer;
  8. -- Obtain the path of a file
  9. Function getpath (filename in varchar2) return varchar2;
  10. -- Obtain a file
  11. Function getfilename (filename in varchar2) return varchar2;
  12. Procedure writefile (filename in varchar2, STR in long );
  13. Function readfile return varchar2;
  14. End ylfileop_pack;
  15. /
  16. Create or replace package body ylfileop_pack is
  17. Isto_file utl_file.file_type; -- above
  18. Procedure setoraclepath (Path in varchar2) is
  19. Sqlstr varchar2 (1024 );
  20. Begin
  21. Sqlstr: = 'create or replace directory' | yl_dir | 'as' | path | '''';
  22. Execute immediate sqlstr;
  23. End;
  24. -- Oracle string from the back to find by yanleigis Email: landgis@126.com
  25. Function lastindex (STR in varchar2, findstr in varchar2) return integer is
  26. I integer;
  27. Num integer;
  28. Subln integer;
  29. Begin
  30. Num: = length (STR );
  31. Subln: = length (findstr );
  32. If subln> num then
  33. Return-1;
  34. End if;
  35. I: = num-subln + 1;
  36. While I> 0 Loop
  37. If substr (STR, I, subln) = findstr then
  38. Return I;
  39. End if;
  40. I: = I-1;
  41. End loop;
  42. Return-1;
  43. End;
  44. -- Obtain the path of a file
  45. Function getpath (filename in varchar2) return varchar2 is
  46. Idx integer;
  47. Begin
  48. Idx: = lastindex (filename ,'/');
  49. If (idx> 0) then
  50. Return substr (filename, 1, idx );
  51. End if;
  52. Return '';
  53. End;
  54. -- Obtain a file
  55. Function getfilename (filename in varchar2) return varchar2 is
  56. Idx integer;
  57. Begin
  58. Idx: = lastindex (filename ,'/');
  59. If (idx> 0) then
  60. Return substr (filename, idx + 1 );
  61. End if;
  62. Return '';
  63. End;
  64. Function openfile (filename in varchar2, state in varchar2) return integer is
  65. Path varchar2 (1024 );
  66. Begin
  67. Path: = getpath (filename );
  68. Setoraclepath (PATH );
  69. Isto_file: = utl_file.fopen (yl_dir, getfilename (filename), State );
  70. Return 1;
  71. Exception
  72. When others then
  73. Dbms_output.put_line (sqlcode | ':' | sqlerrm );
  74. Return 0;
  75. End;
  76. Function openfileread (filename in varchar2) return integer is
  77. Begin
  78. Return openfile (filename, 'R ');
  79. End;
  80. Function openfilewrite (filename in varchar2) return integer is
  81. Begin
  82. Return openfile (filename, 'w ');
  83. End;
  84. Procedure writeline (STR in varchar2) is
  85. Begin
  86. Utl_file.put_line (isto_file, STR); -- write a string
  87. End;
  88. Procedure writelinelong (STR in long) is
  89. Begin
  90. Utl_file.put_line (isto_file, STR); -- write a string
  91. End;
  92. Procedure closefile is
  93. Sqlstr varchar2 (1024 );
  94. Begin
  95. -- Utl_file.fflush (isto_file); -- brush Buffer
  96. Utl_file.fclose (isto_file); -- close the file pointer
  97. Sqlstr: = 'drop directory' | yl_dir;
  98. Execute immediate sqlstr;
  99. End;
  100. Procedure writefile (filename in varchar2, STR in long) is
  101. Begin
  102. If openfilewrite (filename)> 0 then
  103. Writelinelong (STR );
  104. Closefile ();
  105. End if;
  106. End;
  107. Function Readline return varchar2 is
  108. -- Read a row and put it in the fp_buffer variable.
  109. Fp_buffer varchar2 (20000 );--
  110. STR varchar2 (4000 );
  111. Begin
  112. Loop
  113. Begin
  114. Utl_file.get_line (isto_file, STR); -- read a row and put it in the fp_buffer variable.
  115. Fp_buffer: = fp_buffer | STR;
  116. Fp_buffer: = fp_buffer | CHR (13); -- CHR (10)
  117. Exception
  118. When no_data_found then
  119. Exit;
  120. End;
  121. End loop;
  122. Return fp_buffer;
  123. End;
  124. Function readfile return varchar2 is
  125. STR varchar2 (20000 );
  126. Begin
  127. If openfileread ('C:/YL. W')> 0 then
  128. STR: = Readline;
  129. Closefile ();
  130. Return STR;
  131. End if;
  132. Return 'failed to read the file ';
  133. End;
  134. End ylfileop_pack;
  135. /
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.