简体   繁体   中英

UTL_FILE appends LF to end of file

With my following PLSQL block, I experience a Line Feed character at the end of the file. My expectation is that there will be no line feed.

DECLARE
  v_MyFileHandle UTL_FILE.FILE_TYPE;
  BEGIN
       v_MyFileHandle := UTL_FILE.FOPEN('MY_DIREC','HELLO.TXT','a');
       UTL_FILE.PUT(v_MyFileHandle, '1');
       UTL_FILE.FCLOSE(v_MyFileHandle);
 END; 

The above block outputs a file that looks like:

1[LF]

DECLARE
  v_MyFileHandle UTL_FILE.FILE_TYPE;
  BEGIN
       v_MyFileHandle := UTL_FILE.FOPEN('MY_DIREC','HELLO.TXT','a');
       UTL_FILE.PUT(v_MyFileHandle, '1');
       UTL_FILE.PUT(v_MyFileHandle, '2');
       UTL_FILE.FCLOSE(v_MyFileHandle);
 END; 

The above block will produce a file that looks like:

12[LF]

How do I prevent the line feed at the end of the file?

Try DBMS_XSLPROCESSOR.CLOB2FILE as a workaround. It does not create [LF] on Unix and [CR][LF] on Windows at the end of file.

DECLARE
  v_myClob CLOB := '1';
  BEGIN
  DBMS_XSLPROCESSOR.CLOB2FILE( v_myClob, 'MY_DIREC', 'HELLO.TXT');     
 END;

After extensive googling, I found that this worked using "wb" for byte mode and put_raw.

sBufToWrite := 'line1' || chr(13) || chr(10) || line2' || chr(13) || chr(10) || 'line3'; 
sFileName := 'testfile.txt'; 
out_file := utl_file.fopen('DATA_DIRECTORY', sFileName, 'wb'); 
utl_file.put_raw(out_file, utl_raw.cast_to_raw(sBufToWrite)); 
utl_file.fclose(out_file); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM