File operations enable reading and writing files on the SAP application server. The commands OPEN DATASET, READ DATASET, TRANSFER and CLOSE DATASET form the foundation for file processing in ABAP.
Basic Commands
Command Description OPEN DATASETOpen file READ DATASETRead data from file TRANSFERWrite data to file CLOSE DATASETClose file DELETE DATASETDelete file
Syntax
Open File
FOR { INPUT | OUTPUT | APPENDING | UPDATE }
IN { TEXT MODE | BINARY MODE | LEGACY TEXT MODE }
[ ENCODING { DEFAULT | UTF-8 | NON-UNICODE } ]
[ WITH { SMART | NATIVE | UNIX | WINDOWS } LINEFEED ]
Read/Write File
READ DATASET <filename> INTO <data> [ LENGTH <length> ].
TRANSFER <data> TO <filename> [ LENGTH <length> ].
Close/Delete File
CLOSE DATASET <filename>.
DELETE DATASET <filename>.
Examples
1. Write Text File
DATA : lv_file TYPE string VALUE '/tmp/output.txt' ,
OPEN DATASET lv_file FOR OUTPUT IN TEXT MODE
TRANSFER 'Line 1: Hello World' TO lv_file.
TRANSFER 'Line 2: ABAP is great' TO lv_file.
TRANSFER 'Line 3: End of file' TO lv_file.
WRITE : / 'File written successfully' .
WRITE : / 'Error:' , lv_message.
2. Read Text File
DATA : lv_file TYPE string VALUE '/tmp/input.txt' ,
OPEN DATASET lv_file FOR INPUT IN TEXT MODE
READ DATASET lv_file INTO lv_line.
WRITE : / 'File could not be opened' .
3. Read File into Table
DATA : lv_file TYPE string VALUE '/tmp/data.txt' ,
lt_lines TYPE TABLE OF string .
OPEN DATASET lv_file FOR INPUT IN TEXT MODE ENCODING UTF-8 .
READ DATASET lv_file INTO lv_line.
APPEND lv_line TO lt_lines.
WRITE : / 'Lines read:' , lines ( lt_lines ).
4. Write Table to File
DATA : lv_file TYPE string VALUE '/tmp/export.txt' ,
lt_lines TYPE TABLE OF string .
OPEN DATASET lv_file FOR OUTPUT IN TEXT MODE ENCODING UTF-8 .
LOOP AT lt_lines INTO DATA (lv_line).
TRANSFER lv_line TO lv_file.
WRITE : / 'Exported:' , lines ( lt_lines ), 'lines' .
5. Process CSV File
TYPES : BEGIN OF ty_customer,
DATA : lv_file TYPE string VALUE '/tmp/customers.csv' ,
lt_customers TYPE TABLE OF ty_customer,
lt_fields TYPE TABLE OF string .
OPEN DATASET lv_file FOR INPUT IN TEXT MODE ENCODING UTF-8 .
READ DATASET lv_file INTO lv_line.
READ DATASET lv_file INTO lv_line.
SPLIT lv_line AT ';' INTO TABLE lt_fields.
IF lines ( lt_fields ) >= 3 .
APPEND VALUE ty_customer(
age = CONV #( lt_fields[ 2 ] )
LOOP AT lt_customers INTO DATA (ls_cust).
WRITE : / ls_cust-name, ls_cust-age, ls_cust-city.
6. Create CSV Export
DATA : lv_file TYPE string VALUE '/tmp/export.csv' ,
DATA : lt_orders TYPE TABLE OF ty_order.
( id = 1 customer = 'Miller' amount = '100.50' )
( id = 2 customer = 'Schmidt' amount = '250.00' )
( id = 3 customer = 'Weber' amount = '75.25' )
OPEN DATASET lv_file FOR OUTPUT IN TEXT MODE ENCODING UTF-8 .
TRANSFER 'ID;Customer;Amount' TO lv_file.
LOOP AT lt_orders INTO DATA (ls_order).
lv_line = |{ ls_order-id } ; { ls_order-customer } ; { ls_order-amount }| .
TRANSFER lv_line TO lv_file.
7. Read Binary File
DATA : lv_file TYPE string VALUE '/tmp/image.png' ,
lv_buffer TYPE x LENGTH 1024 ,
OPEN DATASET lv_file FOR INPUT IN BINARY MODE .
READ DATASET lv_file INTO lv_buffer LENGTH lv_length.
CONCATENATE lv_data lv_buffer(lv_length) INTO lv_data IN BYTE MODE .
WRITE : / 'Read:' , xstrlen ( lv_data ), 'bytes' .
8. Write Binary File
DATA : lv_file TYPE string VALUE '/tmp/output.bin' ,
" Binary data (e.g., from DB or processing)
lv_data = 'A1B2C3D4E5F6' .
OPEN DATASET lv_file FOR OUTPUT IN BINARY MODE .
TRANSFER lv_data TO lv_file.
9. Append to File (APPENDING)
DATA : lv_file TYPE string VALUE '/tmp/log.txt' ,
lv_line = |{ sy-datum DATE = ISO } { sy-uzeit TIME = ISO } : New entry | .
OPEN DATASET lv_file FOR APPENDING IN TEXT MODE ENCODING UTF-8 .
TRANSFER lv_line TO lv_file.
10. Check and Delete File
DATA : lv_file TYPE string VALUE '/tmp/temp.txt' .
" Check if file exists (by opening)
OPEN DATASET lv_file FOR INPUT IN TEXT MODE ENCODING DEFAULT .
WRITE : / 'File does not exist' .
11. Read Directory
DATA : lv_dir TYPE string VALUE '/tmp/' ,
lt_files TYPE TABLE OF string ,
CALL FUNCTION 'EPS_GET_DIRECTORY_LISTING'
dir_name = CONV eps2path( lv_dir )
build_directory_failed = 3
read_directory_failed = 5
LOOP AT lt_files INTO lv_filename.
12. File with Error Message
DATA : lv_file TYPE string VALUE '/nonexistent/path/file.txt' ,
OPEN DATASET lv_file FOR INPUT IN TEXT MODE
WRITE : / 'Error opening:' .
WRITE : / 'Return Code:' , sy - subrc .
13. Line Break Modes
OPEN DATASET lv_file FOR OUTPUT IN TEXT MODE
" Windows line break (CRLF)
OPEN DATASET lv_file FOR OUTPUT IN TEXT MODE
" Automatic (depending on server)
OPEN DATASET lv_file FOR OUTPUT IN TEXT MODE
" Smart: Write like Native, Read detects automatically
OPEN DATASET lv_file FOR INPUT IN TEXT MODE
14. Positioning in File
DATA : lv_file TYPE string VALUE '/tmp/data.bin' ,
lv_data TYPE x LENGTH 100 ,
OPEN DATASET lv_file FOR UPDATE IN BINARY MODE .
GET DATASET lv_file POSITION lv_pos.
WRITE : / 'Current position:' , lv_pos.
SET DATASET lv_file POSITION 100 .
READ DATASET lv_file INTO lv_data.
15. Process Large Files Efficiently
CONSTANTS : c_buffer_size TYPE i VALUE 8192 .
DATA : lv_file TYPE string VALUE '/tmp/large_file.txt' ,
OPEN DATASET lv_file FOR INPUT IN TEXT MODE ENCODING UTF-8 .
READ DATASET lv_file INTO lv_buffer.
" Process without storing all lines
" process_line( lv_buffer ).
" Show progress (every 10000 lines)
IF lv_count MOD 10000 = 0 .
WRITE : / 'Processed:' , lv_count, 'lines' .
WRITE : / 'Total:' , lv_count, 'lines' .
16. Create Temporary File
DATA : lv_temp_file TYPE string ,
lv_guid TYPE sysuuid_c32.
" Generate unique filename
lv_guid = cl_system_uuid=>create_uuid_c32_static( ).
lv_temp_file = | /tmp/temp_ { lv_guid } .txt | .
OPEN DATASET lv_temp_file FOR OUTPUT IN TEXT MODE ENCODING UTF-8 .
TRANSFER 'Temporary data' TO lv_temp_file.
CLOSE DATASET lv_temp_file.
DELETE DATASET lv_temp_file.
Encoding Options
Encoding Description DEFAULTSystem default UTF-8Unicode (recommended) NON-UNICODELegacy, non-Unicode
sy-subrc Values
Value Meaning 0 Successful 4 End of file (READ) 8 Error (file not found, no authorization)
ABAP Cloud Alternative
" In ABAP Cloud: FILE interface not available
" Instead: CL_ABAP_FILE_UTILITIES or XCO
" Example with XCO (Cloud)
DATA (lo_file) = xco_cp=>file( '/tmp/test.txt' ).
DATA (lv_content) = lo_file->read( )->as_string( ).
Important Notes / Best Practice
Always call CLOSE DATASET – even on errors (TRY-FINALLY).
Use UTF-8 as encoding for modern files.
Use MESSAGE addition for meaningful error messages.
sy-subrc = 4 on READ means end of file (EOF).
APPENDING to append to existing files.
Process large files line by line instead of loading completely.
Check authorizations (S_DATASET) for file access.
Always specify absolute paths (not relative).
In ABAP Cloud , file operations are restricted – use XCO.
Binary files for non-text content (images, PDFs).
Related Articles ABAP Unit Testing: Test-Driven Development Learn to write ABAP Unit Tests: CL_ABAP_UNIT_ASSERT, test classes, Setup/Teardown, Test Doubles, and best practices for high-quality code.
Feb 15, 2026 • ABAP-Statements
ABAP Authorization Checks: AUTHORITY-CHECK Learn authorization checks in ABAP: AUTHORITY-CHECK, authorization objects, roles, and profiles. Develop secure applications.
Feb 15, 2026 • ABAP-Statements
ABAP CDS Annotations: Metadata for Fiori and RAP Learn CDS Annotations in ABAP: UI annotations, OData, Semantics, Search, Analytics, and Metadata Extensions with practical examples.
Feb 15, 2026 • ABAP-Statements