Reading a file stored in application server in SAP ABAP, writing a file to application server in SAP ABAP

    Step1 : OPEN DATASET for Application server SAP
    Step2 : Reading and Writing data from Application Server SAP

Step1:OPEN DATASET for Application server SAP

Most of the times in real-time business application we need to store data in application server for further operations and we need to read data from application server.
In order to do operations on application server, we need to open dataset for a particular file, we use the below syntax to open a data set. 

OPEN DATASET <FILE NAME> FOR OUTPUT IN <MODE> MODE ENCODING DEFAULT.
**Do file operations like writing file, reading file
CLOSE DATASET <FILE NAME>. "Close data set for file
 
In the above syntax <FILE NAME> is the name of the application server file, <MODE> is the out put mode like BINARY MODE or TEXT MODE or LEGACY BINARY MODE or LEGACY TEXT MODE.
In the next step, example program explains you how to read data from application server and how to write data to application server.

 

Step2:Reading and Writing data from Application Server SAP

The below example program explains you of writing data into application server using OPEN DATASET and reading data from application server using OPEN DATASET.
REPORT ZSAPN_APPLICATION_SERVER.

PARAMETERS FILE TYPE string DEFAULT 'testtemp.txt' .
PARAMETERS P_MTART TYPE MARA-MTART.

DATA IT_MARA TYPE TABLE OF MARA.
DATA WA_MARA TYPE MARA.
DATA LV_STRING TYPE STRING.
SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS
  WHERE MTART = P_MTART .
OPEN DATASET FILE FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

LOOP AT IT_MARA INTO WA_MARA .

  CONCATENATE WA_MARA-MATNR WA_MARA-MTART WA_MARA-MEINS INTO LV_STRING.
  TRANSFER LV_STRING TO FILE.
ENDLOOP.

CLOSE DATASET FILE.

DATA LV_DATA TYPE STRING.

OPEN DATASET FILE FOR INPUT IN TEXT MODE ENCODING DEFAULT.
DO.
  READ DATASET FILE INTO LV_DATA.
  IF SY-SUBRC <> 0.
  EXIT.
  ELSE.
  WRITE:/ LV_DATA.
  ENDIF.

ENDDO.
CLOSE DATASET FILE.
 
 
Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP MM SD Consultant
Contact : serkurumsal@yandex.com