File Layout's
- It is used for file processing using INBOUND and OUTBOUND process
- A File layout is a collection of records.
- Works with hierarchical and non-hierarchical data
- stored with _FLT
- It enables us to do the following
- Export and Import the data.
- It uses the Batch Process to perform a large amount of data import and export.
- There are 3 types of file formats.They are CSV,XML,FIXED.
- It also facilitates to provide the transaction between PS systems and 3rd party systems.
Inbound Program
Reading from a FALT file and writing into the database
- Open the flat file in READ mode
- Read each and every line in a flat file and put them in a string
- Split the data with separators and put it in an array
- Insert the values into the record by matching the field values into a record.
Example:
Local record &rec
&myfile=getfile("c:/documents/abcd.txt","R",%filepath_absolute);
&arr=createarrayrept("",0);
&rec=createrecord(record.EMP_TBL);
If &file.open() then
while &file.readline(&str);
&arr = split(&str,",");
&rec.EMP_ID.value = &arr[1];
&rec.EMP_NAME.value = &arr[2];
&rec.EMP_AGE.value = &arr[3];
&rec.insert();
End-while;
end-if;
&myfile.close
Outbound Program
Writing from a Database to flat file
Open the flat file in WRITE mode.
Use the %SELECT to select all the record field values.
Put the field values in a dummy record.
By using the dummy record field value write the files to a FLAT file using WRITELINE.
Example:
&myfile=getfile("c:/documents/abcd.txt","W",%filepath_absolute);
&myfile.open("c:/documents/abcd.txt","W");
For i=1 to5;
&myfile.writeline("HI welcome to PS world");
end-for;
&myfile.close
A sample code to read a file using file layouts and insert data read into a record
Local File &MYFILE;
Local Record &REC;
Local array of string &ARRAY;
Local string &FILE_DIRECTORY, &FileName;
&FileName = "MY_FILE_NAME.txt";
&FILE_DIRTORY = "/MYDIRECTORY/"
/*open file for reading*/
&MYFILE = GetFile(&FILE_DIRECTORY | &FileName, "R", %FilePath_Absolute);
/*create record object*/
&REC = CreateRecord(Record.MY_RECORD);
&ARRAY = CreateArrayRept("", 0);
/*check if file is open*/
If &MYFILE.IsOpen Then
/*The SetFileLayout method is a file layout method. It associates a specific file layout definition with the file object executing this method, providing easy access to rowset data.(PeopleBooks)*/
If &MYFILE.SetFileLayout(FileLayout.FILE_LAYOUT_NAME) Then
/*read line into &STRING*/
While &MYFILE.ReadLine(&STRING);
&ARRAY = Split(&STRING, ",");
For &I = 1 To &REC.FieldCount
&REC.GetField(&I).Value = RTrim(LTrim(&ARRAY [&I]));
End-For;
/* do additional processing here for converting values */
&REC.Insert();
/*count rows inserted into record*/
&COUNT = &COUNT + 1;
End-While;
Else
/* do error processing - filelayout not correct */
End-If;
Else
/* do error processing - file not open */
End-If;
No comments:
Post a Comment