11.3 File Interfaces
In addition to storing data in the database layer, you can save data in a persistent form in the application and presentation layers. Data is stored as sequential files in these layers—for example, as spreadsheets. In this section, you’ll learn about working with files in the presentation application layers.
We’ll first look at how sequential files can be saved to the application server directories from the ABAP program and how to read the saved files to be processed in ABAP programs in Section 11.3.1. Then, in Section 11.3.2, we’ll discuss exporting and importing files from and to the presentation layer.
11.3.1 Working with Files in the Application Server
We use different ABAP statements to transfer data to a file stored in the application server directory. We typically perform two kinds of operations with files stored in the application server directory:
- Read the file from the application server directory to an ABAP program (inbound interface)
- Transfer the file from the ABAP program to the application server directory (outbound interface)
In an inbound interface, the data is read from the file and transferred to an internal table for further processing in the ABAP program. Conversely, in an outbound interface, the data from an internal table in the ABAP program is transferred to the file in the application server.
The following ABAP statements are used to read and write the files in the application server:
-
OPEN DATASET
Opens the file for access. The system performs certain implicit authorization checks to ensure the user has the required permissions to access the file. Up to one hundred files can be opened per internal session. This statement will result in a catchable exception if the file is already opened in the program. The access type for which the file is to be opened should be supplied. The file can be opened for the following:- For Input: Opens the file for reading.
- For Output: Opens the file for writing
- Appending: Opens the file for appending data. If the file exists, the file is opened and the cursor points to the end of the line to append new lines. If the file doesn’t exist, a new file is created.
- Update: Opens the file for modifying the existing content.
The following statement shows an example of using the OPEN DATASET statement to open a file for writing:
OPEN DATASET v_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
Here, v_file contains the path of the file in the application server. directory. -
TRANSFER
If the file isn’t opened for INPUT, data from a data object can be transferred to the file using this statement. Before this statement can be used, the file should be opened using the OPEN DATASET statement. If the file isn’t opened, it will result in a catchable exception.
Listing 11.11 shows the sample code to transfer the records to file in the application server.If the file is opened in text mode, the data object should be character type. If the file is opened in binary mode, only binary data should be transferred.OPEN DATASET v_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT itab INTO wa.
TRANSFER wa TO v_file.
ENDLOOP.
CLOSE DATASET v_file. -
READ DATASET
This statement transfers the data from the file to the program data object one line at a time. The file should be opened for input before using the READ DATASET statement. This statement is used in a DO loop to read all the lines of the file. Listing 11.12 shows example code using the READ DATASET statement.OPEN DATASET v_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.
DO.
READ DATASET v_file into wa_makt.
IF sy-subrc IS NOT INITIAL.
EXIT.
ELSE.
APPEND wa to itab.
ENDIF.
ENDDO.
CLOSE DATASET v_file. -
GET DATASET
Gets the current cursor position in the file. -
SET DATASET
Sets the cursor in the file. The position is specified explicitly or can be set as the end of the file. -
TRUNCATE DATASET
Sets the end of the file. -
CLOSE DATASET
Closes the file. If the file isn’t closed explicitly using this statement, it’s closed automatically when the program is closed. -
DELETE DATASET
Deletes the file. The system performs implicit checks to ensure the user has the required authorizations to delete the file.
The file names are operating system-dependent because they’re physical names. We recommend using platform-independent file names to make your programs portable.
To make your file names platform-independent, use logical file names and paths. These logical file names and paths are linked to physical file names and paths using special tables in customizing that can be maintained using Transaction FILE. The physical file name from a logical file name is generated using the FILE_GET_NAME function module.
Using logical file names and paths also enhances security.
11.3.2 Working with Files in the Presentation Layer
Sometimes, users may want to save data locally to their workstations or upload data to a program for processing. For example, a user may want to download the report output as a PDF file or upload a spreadsheet with material data to be posted in the database. In such scenarios, we can use the CL_GUI_FRONTEND_SERVICES global class to work with the files in the presentation layer.
Among other methods, the class provides the GUI_UPLOAD and GUI_DOWNLOAD static methods to upload or download data from or to the user’s workstation. The class documentation available for these methods in Class Builder explains their usage clearly.
To download the file to the presentation layer, the file path, file type (binary or ASCII), and internal table that contains the data are passed to the GUI_DOWNLOAD method.
To upload a file, an internal table is defined with the number of fields matching the number of columns in the file. This table is then passed to the GUI_UPLOAD method along with the file path. The data will be uploaded from the file to the supplied internal table.
Listing 11.13 shows sample code using the GUI_UPLOAD method.
TYPES : BEGIN OF ty_file,
field1 TYPE char20,
field2 TYPE char30,
field3 TYPE char10,
END OF ty_file.
DATA : it_file TYPE STANDARD TABLE OF ty_file.
PARAMETERS p_file type string.
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = p_file
filetype = 'ASC'
has_field_separator = SPACE
CHANGING
data_tab = it_file
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
not_supported_by_gui = 17
error_no_gui = 18
others = 19
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
In this section, we looked at how to save data persistently in the application and presentation layers. In the next section, we’ll discuss what data clusters are and how to export and import data by using them.