Read & Write Operations on App.Server Files Using CL_RSAN_UT_APPSERV_FILE_READER

Class Methods used in the sample Code

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.


*&--------------------------------------------------------------------
*& 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.
REFRESH : lt_file_table.
CLEAR   : ls_file_table.
IF lt_makt[] IS NOT INITIAL.
LOOP AT lt_makt INTO ls_makt.
CLEAR : ls_file_table.
CONCATENATE ls_makt-matnr
ls_makt-maktx INTO ls_file_table
SEPARATED BY '|'.
APPEND ls_file_table TO lt_file_table.
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

你可能感兴趣的:(ext)