Skip to main content

Module 14: File Handling & Data Transfer

File handling in ABAP is used for data exchange with external systems, batch processing, and integration scenarios.
Understanding where the file resides (application server vs presentation server) is critical for correct design.


1. File Locations in SAP


1.1 Application Server Files

Files stored on the SAP application server filesystem.

  • Accessible by background jobs
  • Suitable for batch processing
  • Independent of user PC

Typical directories:

  • /usr/sap/<SID>/SYS/global
  • Logical paths via FILE transaction
Best Practice

Always use logical file paths, never hard-coded physical paths.


1.2 Presentation Server Files

Files stored on the user’s local machine.

  • Accessible only in dialog mode
  • Requires user interaction
  • Not usable in background jobs
Limitation

Presentation server files cannot be used in background processing.


2. OPEN DATASET – Application Server Files

OPEN DATASET is used for file operations on the application server.


2.1 Opening a File

OPEN DATASET lv_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.

Modes:

  • FOR INPUT – Read

  • FOR OUTPUT – Write (overwrite)

  • FOR APPENDING – Append

2.2 Reading a File

DATA lv_line TYPE string.

READ DATASET lv_file INTO lv_line.

2.3 Writing to a File

TRANSFER lv_line TO lv_file.

2.4 Closing the File

CLOSE DATASET lv_file.
Always Close Files

Failing to close datasets can cause file locks and resource leaks.

3. Modern OPEN DATASET Pattern (7.5+)

OPEN DATASET lv_file FOR INPUT IN TEXT MODE ENCODING UTF-8.

WHILE sy-subrc = 0.
READ DATASET lv_file INTO DATA(lv_line).
IF sy-subrc <> 0.
EXIT.
ENDIF.
" Process lv_line
ENDWHILE.

CLOSE DATASET lv_file.
Encoding Rule

Always specify encoding (prefer UTF-8) to avoid character corruption.

4. Presentation Server File Handling

For local files, SAP provides frontend services.

4.1 File Upload (GUI Upload)

cl_gui_frontend_services=>gui_upload(
EXPORTING
filename = lv_file
filetype = 'ASC'
CHANGING
data_tab = lt_data
).

4.2 File Download (GUI Download)

cl_gui_frontend_services=>gui_download(
EXPORTING
filename = lv_file
filetype = 'ASC'
CHANGING
data_tab = lt_data
).
Modern Rule

Use CL_GUI_FRONTEND_SERVICES instead of obsolete function modules like GUI_UPLOAD.

5. CSV / Flat File Handling

5.1 Reading CSV from Application Server

SPLIT lv_line AT ',' INTO TABLE DATA(lt_fields).

5.2 Writing CSV Data

DATA(lv_csv_line) = |{ ls_data-field1 },{ ls_data-field2 }|.
TRANSFER lv_csv_line TO lv_file.

5.3 Handling Headers

IF lv_line CP 'FIELD1*'.
CONTINUE.
ENDIF.
CSV Best Practice
  • Handle headers explicitly

  • Escape delimiters

  • Validate number of fields

6. Error Handling in File Operations

OPEN DATASET lv_file FOR INPUT.
IF sy-subrc <> 0.
RAISE EXCEPTION NEW zcx_file_error(
iv_text = 'Unable to open file'
).
ENDIF.
Do Not Ignore sy-subrc

File operations fail silently if sy-subrc is ignored.

  1. Performance & Security Considerations
Follow These Rules
  • Avoid large files in dialog mode

  • Use background jobs for bulk files

  • Validate file content

  • Restrict file access via logical paths

  1. Common Mistakes
Avoid These
  • Hard-coded file paths

  • Using presentation server files in background jobs

  • Ignoring encoding

  • Not closing datasets

  • Reading large files into memory at once

9. Summary

  • Application server files → OPEN DATASET

  • Presentation server files → CL_GUI_FRONTEND_SERVICES

  • Always use logical paths

  • Specify encoding

  • Handle CSV/flat files carefully

  • Validate and secure file operations

10. Practice Exercises

  • Read a CSV file from application server.

  • Write processed data to another file.

  • Upload a local file and display data in ALV.

  • Implement error handling for missing files.

  • Convert flat file data into internal tables.

11. What’s Next?

➡️ Module 15: SAP Enhancements & Modifications

Learning Tip

File handling is critical for interfaces, batch jobs, and integrations.