当前位置: 代码迷 >> 应用服务器 >> abap 向应用服务器读写资料
  详细解决方案

abap 向应用服务器读写资料

热度:7696   发布时间:2013-02-26 00:00:00.0
abap 向应用服务器读写文件
应用服务器文件操作语句及其功能:

语句功能说明
OPEN DATASET打开文件打开一个文件以供后续读取操作;或打开一个文件以供后续写入操作
READ DATASET读取文件操作从文件中读取数据岛程序的数据结构中
TRAMSFER将数据写入文件将程序中的数据写入文件
CLOSE DATASET关闭文件关闭文件,标识文件处理结束
DELETE DATASET删除文件删除应用服务器上的数据文件


写入文件程序
*&---------------------------------------------------------------------*
*& Report  ZTCXHTEST_WRITE_FILE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZTCXHTEST_WRITE_FILE.
DATA: fname(60) TYPE c VALUE 'samplefile.txt'.
TYPES:  BEGIN OF line,
              col1(1) TYPE c,
              col2(10)  TYPE c,
              END OF line.

TYPES itab TYPE line OCCURS 10.
DATA: lin TYPE  line,
           tab  TYPE itab.

DO 5 TIMES.
  lin-col1 = sy-index.
  lin-col2 = sy-index ** 2.
  APPEND lin TO tab.
ENDDO.

OPEN DATASET fname FOR OUTPUT IN TEXT MODE ENCODING UTF-8.
LOOP AT tab INTO lin.
  TRANSFER lin TO fname.
ENDLOOP.

CLOSE DATASET fname.

----------------------------------------------
读取文件
*&---------------------------------------------------------------------*
*& Report  ZTCXHTEST_READ_FILE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZTCXHTEST_READ_FILE.
DATA: fname(60) TYPE c VALUE 'samplefile2.txt',
          text1(12) TYPE c VALUE 'abcdefghijkl',
          text2(5)  TYPE c,
          len TYPE i.
OPEN DATASET fname FOR OUTPUT IN BINARY MODE.
TRANSFER text1 TO fname.
CLOSE DATASET fname.

OPEN DATASET fname FOR INPUT IN BINARY MODE.
DO.
  READ DATASET fname   INTO text2 LENGTH len.
  WRITE:  / sy-subrc, text2,  len.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
ENDDO.

CLOSE DATASET fname.

------------------------------------------------
*&---------------------------------------------------------------------*
*& Report  ZTCXHTEST_OPERATE_ON_APPSERVER 将结构体写入文件
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZTCXHTEST_OPERATE_ON_APPSERVER.
TABLES bkpf.

DATA dsn(20) TYPE c VALUE 'bbkpf.dat'.
DATA rec LIKE bbkpf.

OPEN DATASET dsn FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc <> 0.
  EXIT.
ENDIF.

SELECT * FROM bkpf UP TO 10 ROWS.
  CLEAR rec.
  rec-stype = '1'.
  rec-tcode = 'FB01'.
  rec-bldat = sy-datum.
  rec-bukrs = bkpf-bukrs.
  rec-belnr = bkpf-belnr.
  rec-blart = bkpf-blart.
  TRANSFER rec TO dsn.
ENDSELECT.

CLOSE DATASET dsn.
OPEN DATASET dsn FOR INPUT IN TEXT MODE ENCODING DEFAULT.

DO.
  READ DATASET dsn INTO rec.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
  WRITE:  / rec.
ENDDO.

CLOSE DATASET dsn.