Bender
Bender

Reputation: 371

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?

Upvotes: 0

Views: 1834

Answers (2)

Bender
Bender

Reputation: 371

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); 

Upvotes: 1

dcieslak
dcieslak

Reputation: 2715

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;

Upvotes: 1

Related Questions