QSYS2.IFS_WRITE is a system-provided function in IBM i that allows you to write data to the Integrated File System (IFS). The IFS is the file system on IBM i that provides a UNIX-like file structure and allows access to files and directories.
The QSYS2.IFS_WRITE function can be used to write data to a file in the IFS. It takes the following parameters:
Here's an example of how you might use the QSYS2.IFS_WRITE function in SQL:
CALL QSYS2.IFS_WRITE
LINE => 'This is some data to be written to the file',
OVERWRITE => 'REPLACE',
END_OF_LINE => 'NONE');
-- Add lines to the output file
FOR SELECT OBJNAME AS LIBNAME FROM TABLE(QSYS2.OBJECT_STATISTICS('*ALLSIMPLE', 'LIB')) DO
CALL QSYS2.IFS_WRITE(PATH_NAME => '/tmp/library_names',
LINE => LIBNAME, END_OF_LINE => 'CRLF');
This would write the string "This is some data to be written to the file" to the file located at /tmp/mystuff.txt in the IFS.
RPG Example
Let's read a file and write the contents into an IFS location with each row of data on a new line.
The file is MYFILE which contains one field called MYDATA

A simple RPG to read all the rows in this file, writing to an IFS location might look like this:
/title Simple RPG program to write to the IFS using SQL
// ----------------------------------------------------------
//
// Service - WRITEIFS.RPGLE
//
// Function - Simple RPG program to write to the IFS using SQL
//
// COMPILE NOTES:
//
// Obviously change the source location to match yours:
//
// CRTSQLRPGI OBJ(NICKLITTEN/WRITEIFS)
// SRCSTMF('/home/nicklitten/source/writeifs.sqlrpgle')
// COMMIT(*NONE)
// OBJTYPE(*PGM)
// DBGVIEW(*SOURCE)
// CVTCCSID(*JOB)
//
// Modification History:
// 2020-06-31 V1.0 Created by Nick Litten
// ----------------------------------------------------------
ctl-opt
main(WRITEIFS)
option(*srcstmt:*nodebugio:*noshowcpy)
/if defined(*CRTSQLRPGI)
dftactgrp(*no) actgrp('NICKLITTEN')
/endif
copyright('WRITEIFS.SQLRPGLE: Version 1.0 June 2020');
dcl-proc WRITEIFS;
dcl-pi WRITEIFS end-pi;
// Status Message string for humans
dcl-s stsMsg char(50);
// IFS Location where the data will be written
dcl-s ifsFile varchar(255) inz('/home/nicklitten/myfile.txt');
dcl-s ifsData varchar(255);
dcl-s overwrite varchar(10) inz('REPLACE');
monitor;
// Set SQL option, mainly to force cursor to close at endmodule
exec sql
set option naming = *sys,
commit = *none,
usrprf = *user,
dynusrprf = *user,
datfmt = *iso,
closqlcsr = *endmod;
exec sql
declare c1 cursor for
select MYDATA
from MYFILE;
exec sql open c1;
// Keep reading until end of file (or an error occurs)
dou sqlCode <> 0;
exec sql fetch c1 into :ifsData;
Select;
when sqlcode = 0;
exec sql CALL QSYS2.IFS_WRITE
(PATH_NAME =>:ifsFile,
LINE => :ifsData,
OVERWRITE => :overwrite,
FILE_CCSID => 1208,
END_OF_LINE => 'CRLF');
// the first write is OVERWRITE to clear the file
// but all others lets APPEND so we add new lines
overwrite = 'APPEND';
stsMsg = 'Completed normally - read next row';
when sqlcode = 100;
stsMsg = 'No data found';
leave;
when sqlcode > 0;
stsMsg = 'Completed with warning';
leave;
when sqlcode < 0;
stsMsg = 'Did not complete normally';
leave;
endsl;
enddo;
// close the cursor
exec sql close c1;
on-error ;
dump(a);
dsply ('*** WRITEIFS has failed!');
endmon ;
return;
end-proc;

When LINE parameter is longer than 32000 characters it fails . How can be saved up to 2GB ?
While 2 GB is the technical limit, writing such large chunks in one go may impact performance. For better control and error handling, consider breaking the data into smaller segments. You could do multiple writes to the IFS file location ADDING each new 2gb chunk.
IBM i also provides:
QSYS2.IFS_WRITE_BINARY – for binary data
QSYS2.IFS_WRITE_UTF8 – for UTF-8 encoded text
These share the same 2 GB per-call limit and similar parameters.