Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Read & Write Operations on App.Server Files Using CL_RSAN_UT_APPSERV_FILE_READER and CL_RSAN_UT_APPSERV_FILE_WRITER

 

Summary

This Post narrates the usage of class CL_RSAN_UT_APPSERV_FILE_READER and CL_RSAN_UT_APPRSERV_WRITER for read/write operations on Application Server Files.

Author(s):  Vinod Kumar T

   
Company:     NIIT Technologies Limited
Created on:    22.09.2010
Author(s) Bio
Vinod Kumar is an ABAP consultant working with NIIT Technologies Limited.

Class Methods used in the sample Code

Class Name

Method

Description

CL_RSAN_UT_FILES

F4

F4 Help for Choosing File Name from GUI or App. Server

CL_RSAN_UT_APPSERV_FILE_WRITER

APPSERVER_FILE_WRITE

Write Data to Specified File on Application Server

CL_RSAN_UT_APPSERV_FILE_READER

APPSERVER_FILE_READ

Read Specified File from Application Server

Sample Code

 

One Form routine in sample code narrates how the list materials selected from MAKT table can be written to application server file.  Another form-routine narrates how to read the contents of file using the methods listed in above table.

Error rendering macro 'code': Invalid value specified for parameter 'lang'
*&---------------------------------------------------------------------
* *& REPORT  ZVK_CL_RSAN_UT_APPSERV
* *&
*&---------------------------------------------------------------------
* *& PURPOSE : USE OF CLASS CL_RSAN_UT_APPSERV_FILE_READER &
* *&           CL_RSAN_UT_APPSERV_FILE_WRITER FOR READ/WRITE OPERATIONS
* *&           ON APPLICATION SERVER FILES
*&---------------------------------------------------------------------
 REPORT  ZVK_CL_RSAN_UT_APPSERV.
* ** TEXT ELEMENTS
* * P_MATNR   MATERIAL FROM  TO
* * P_FILENM  - FILE NAME
 TYPE-POOLS : RSANM, ABAP.
 TABLES : MARA.
 TYPES : BEGIN OF TY_MAKT,
            MATNR TYPE MATNR,
            MAKTX TYPE MAKTX,
          END OF TY_MAKT.
 DATA : LT_MAKT TYPE STANDARD TABLE OF TY_MAKT,
         LS_MAKT TYPE TY_MAKT,
         LT_FILE_TABLE TYPE RSANM_FILE_TABLE,
         LS_FILE_TABLE TYPE RSANM_FILE_LINE.
 DATA : LV_APPLSERV         TYPE CHAR01,
         LV_TITLE            TYPE STRING,
         LV_GUI_EXTENSION    TYPE STRING,
         LV_GUI_EXT_FILTER   TYPE STRING,
         LV_CANCELED         TYPE AS4FLAG,
         LV_APPLSERV_LOGICAL TYPE AS4FLAG,
         LV_APPLSERV_AL11    TYPE AS4FLAG,
         LV_FILE_NAME        TYPE STRING,
         LV_LINES_WRITTEN    TYPE I.
 SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME.
 SELECT-OPTIONS : P_MATNR FOR MARA-MATNR OBLIGATORY.
 SELECTION-SCREEN END OF BLOCK B1.
 SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME.
 PARAMETERS : P_FILENM LIKE IBIPPARMS-PATH OBLIGATORY.
 SELECTION-SCREEN END OF BLOCK B2.
 AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILENM.
   CLEAR : LV_CANCELED , LV_FILE_NAME.
   MOVE 'X' TO LV_APPLSERV.
   MOVE 'Select File from Application Server' TO LV_TITLE.
   MOVE ' ' TO LV_APPLSERV_LOGICAL.
   MOVE 'X' TO LV_APPLSERV_AL11.
   CALL METHOD CL_RSAN_UT_FILES=>F4
     EXPORTING
       I_APPLSERV         = LV_APPLSERV
       I_TITLE            = LV_TITLE
       I_GUI_EXTENSION    = LV_GUI_EXTENSION
       I_GUI_EXT_FILTER   = LV_GUI_EXT_FILTER
       I_APPLSERV_LOGICAL = LV_APPLSERV_LOGICAL
       I_APPLSERV_AL11    = LV_APPLSERV_AL11
     IMPORTING
       E_CANCELED         = LV_CANCELED
     CHANGING
       C_FILE_NAME        = LV_FILE_NAME
     EXCEPTIONS
       FAILED             = 1
       OTHERS             = 2.
   IF SY-SUBRC <> 0.
     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
   ELSE.
     IF LV_CANCELED NE 'X'.
       MOVE LV_FILE_NAME TO P_FILENM.
     ENDIF.
   ENDIF.
 START-OF-SELECTION.
   MOVE P_FILENM TO LV_FILE_NAME.
   PERFORM EXTRACT_DATA.
   PERFORM CONVERT_DATA.
   PERFORM WRITE_DATA_TO_APPSERVER.
   PERFORM READ_DATA_FROM_APPSERVER.
*  &---------------------------------------------------------------------
*  *&      FORM  EXTRACT_DATA
*  &---------------------------------------------------------------------
*  *       TEXT
*  ----------------------------------------------------------------------
 FORM EXTRACT_DATA.
   REFRESH : LT_MAKT.
   SELECT MATNR MAKTX
     FROM MAKT
     INTO TABLE LT_MAKT
     WHERE MATNR IN P_MATNR
     AND   SPRAS EQ SY-LANGU.
   IF SY-SUBRC NE 0.
     MESSAGE 'No data selected' TYPE 'I' DISPLAY LIKE 'E'.
     STOP.
   ENDIF.
 ENDFORM.                    "extract_data
*&---------------------------------------------------------------------
* *&      FORM  CONVERT_DATA
* &---------------------------------------------------------------------
* *       TEXT
* ----------------------------------------------------------------------
 FORM CONVERT_DATA.
   FIELD-SYMBOLS:<FS> TYPE ANY.
   FIELD-SYMBOLS:<FS1> TYPE ANY.
   DATA:LV_LENGTH TYPE I,
         WF_REF TYPE REF TO DATA.
   REFRESH : LT_FILE_TABLE.
   CLEAR   : LS_FILE_TABLE.
   IF LT_MAKT[] IS NOT INITIAL.
     LOOP AT LT_MAKT INTO LS_MAKT.
       DO.
         ASSIGN COMPONENT SY-INDEX OF STRUCTURE LS_MAKT TO <FS>.
         IF SY-SUBRC <> 0.
           APPEND LS_FILE_TABLE TO LT_FILE_TABLE.
           CLEAR LS_FILE_TABLE.EXIT.
         ENDIF.
         LV_LENGTH = 0.
         DESCRIBE FIELD <FS> OUTPUT-LENGTH LV_LENGTH.
         CREATE DATA WF_REF TYPE C LENGTH LV_LENGTH.
         IF WF_REF IS BOUND.
           ASSIGN WF_REF->* TO <FS1>.
           CHECK <FS1> IS ASSIGNED.
           <FS1> = <FS>.
         ENDIF.
         IF SY-INDEX = 1.
           LS_FILE_TABLE = <FS1>.
         ELSE.
           CONCATENATE LS_FILE_TABLE <FS1> INTO LS_FILE_TABLE SEPARATED BY '|'.
         ENDIF.
       ENDDO.
     ENDLOOP.
   ENDIF.
 ENDFORM.                    "convert_data
*&---------------------------------------------------------------------
* *&      FORM  WRITE_DATA_TO_APPSERVER
*&---------------------------------------------------------------------
* * FORM ROUTINE FOR CREATING THE FILE IN APPLICATION SERVER
* * =========================================================
* * PARAMETER I_OVERWRITE HAS RELEVANT IF AN EXISTING FILE IS CHOSEN TO
* * WRITE THE CONTENTS
* * IF VALUE OF PARAMETER I_OVERWRITE IS 'X' THEN FILE IS OVERWRITTEN
* * OTHERWISE THE DATA IS APPENDED TO EXISTING FILE.
* * =========================================================
* ----------------------------------------------------------------------
 FORM WRITE_DATA_TO_APPSERVER.
   IF LT_FILE_TABLE[] IS NOT INITIAL.
     CLEAR : LV_LINES_WRITTEN.
     CALL METHOD CL_RSAN_UT_APPSERV_FILE_WRITER=>APPSERVER_FILE_WRITE
       EXPORTING
         I_FILENAME      = LV_FILE_NAME
         I_OVERWRITE     = ABAP_TRUE
         I_DATA_TAB      = LT_FILE_TABLE
       IMPORTING
         E_LINES_WRITTEN = LV_LINES_WRITTEN
       EXCEPTIONS
         OPEN_FAILED     = 1
         WRITE_FAILED    = 2
         CLOSE_FAILED    = 3
         OTHERS          = 4.
     IF SY-SUBRC <> 0.
       MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                  WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
     ELSE.
       WRITE :/ 'Data written to ',
                LV_FILE_NAME.
       WRITE :/ 'No of Lines Written ',
                LV_LINES_WRITTEN.
     ENDIF.
   ENDIF.
 ENDFORM.                    "write_data_to_appserver
*&---------------------------------------------------------------------
* *&      FORM  READ_DATA_FROM_APPSERVER
* &---------------------------------------------------------------------
* * FORM ROUTINE TO READ THE CONTENTS OF THE FILE IN APPLICATION SERVER
* ----------------------------------------------------------------------
 FORM READ_DATA_FROM_APPSERVER.
   REFRESH : LT_FILE_TABLE.
   CALL METHOD CL_RSAN_UT_APPSERV_FILE_READER=>APPSERVER_FILE_READ
     EXPORTING
       I_FILENAME   = LV_FILE_NAME
     CHANGING
       C_DATA_TAB   = LT_FILE_TABLE
     EXCEPTIONS
       OPEN_FAILED  = 1
       READ_FAILED  = 2
       CLOSE_FAILED = 3
       OTHERS       = 4.
   IF SY-SUBRC <> 0.
     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
   ELSE.
     WRITE :/ 'Contents of file ', LV_FILE_NAME.
     WRITE :/ '================================='.
     LOOP AT LT_FILE_TABLE INTO LS_FILE_TABLE.
       WRITE :/ LS_FILE_TABLE.
     ENDLOOP.
   ENDIF.
 ENDFORM.                    "read_data_from_appserver


 

Useful Information

Class & Methods to Read/Write Operations on Application Server Files

1 Comment

  1. Thanks Vinod. Very good info. I was not even knowing about these classes. But still there is no option to mention the code page.

    Now i can get rid of my utility development available here  (wink)

    http://wiki.sdn.sap.com/wiki/display/stage/Function+Module+to+Upload_Download+files+from_to+Server

    BR,

    keshav