You can read and write files from the application server using the OPEN DATASET command.
You cannot use the internal table as you would with a local one, you have to write or read one line at a time.
Also, when linking with an external system, it is necessary to confirm whether it is fixed length or variable length, and when it is variable length, whether it is tab delimited or comma delimited.
TYPES: BEGIN OF t_file, data(1000) TYPE c, END OF t_file. TYPES: BEGIN OF t_data, col1(10) TYPE c, col2(10) TYPE c, col3(10) TYPE c, END OF t_data. DATA: v_file TYPE string, v_cnt TYPE i, wa_file TYPE t_file, it_file TYPE TABLE OF t_file, wa_data type t_data, it_data type table of t_Data. CONSTANTS: c_tab type c value cl_abap_char_utilities=>horizontal_tab. v_file = '/usr/sap/trans/test.txt'. OPEN DATASET v_file FOR INPUT IN TEXT MODE ENCODING DEFAULT WITH WINDOWS LINEFEED. IF sy-subrc <> 0. * MESSAGE XXX WITH v_file. ENDIF. DO. READ DATASET v_file INTO wa_file. CASE sy-subrc. WHEN 0. APPEND wa_file TO it_file. WHEN 4. EXIT. ENDCASE. ENDDO. CLOSE DATASET v_file. LOOP AT it_file INTO wa_file. v_cnt = v_cnt + 1. * Ignore header record IF v_cntExplanation
First, open the file with the OPEN DATASET command. An error will occur if the file is opened by another program.
This time, it is described as WINDOWS LINE FEED, but this is because the line feed code is CRLF. If the line feed code is LF, you need to use UNIX LINE FEED.
If the file cannot be read, check the ENCODING specification and the character code of the file.
If the file can be read, one line is read by the READ DATASET command and writing to the internal table is repeated.
Please note that the value acquired at this time is flat data. After that, you have to remove or delimit the header according to the definition.Sample code:Write
OPEN DATASET v_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT WITH WINDOWS LINEFEED. IF sy-subrc <> 0. * MESSAGE XXX WITH v_file. ENDIF. LOOP AT it_data INTO wa_data. CONCATENATE wa_data-col1 wa_data-col2 wa_data-col3 INTO wa_file SEPARATED BY c_tab. TRANSFER wa_file TO v_file. ENDLOOP.Explanation
Similarly, open the file with the OPEN DATA SET command. It doesn’t matter if it doesn’t exist.
Next, you can write it to a file by processing the values in the internal table into flat data (tab delimited this time) and using the TRANSFER command.