Overview
IO Services allows developers to write the batch jobs which
involves reading and writing of large files of different formats
very quickly and efficiently. While writing the batch jobs which involves
working with files of different formats like fixed width flat, delimited flat,
XML and OFX, developers can concentrate more on the business logic than the
core tasks like parsing and generating of the files. IO API
parses the required file and return the records in the file as java objects and
accepts the java objects represents the records to generate the file.
IO API requires some information from the developers to understand the file structure that
it is going to prase and generate.
It accepts this information in the format of XML and this is called the file spec.
This file spec explains the details like file format and information to identify
the records and fields in the file.
Understanding Fixed Width flat file spec
As mentioned in the Overview section, file spec will be defined in XML format
and tells the structure of the file, information to identify the records and fields
in those records.
All the fixed width flat file specs will have the following XML format.
<file-spec file-type="fixed-width-flat">
<record-spec record-type="DETAIL" starts-with="5" record-length="42">
<field-spec field-name="field-name1" start-pos="12" end-pos="23"/>
<-- More field specs will follow -->
</record-spec>
<-- More records specs will follow -->
</file-spec>
In this file spec, file-spec element provides the information about the type of the file. For
all the fixed width flat files the file-type attribute value should be fixed-width-flat.
record-spec element provides the information to identify the record in the file. Fixed width
flat file record-spec requires the attributes "record-type", "starts-with" and "record-length".
"record-type" attribute value can be anything that you want to use in your java code,
"starts-with" attribute value tells the parser that each record of that type should starts with
that values, and "record-length" attribute values tells the length of the record of that type.
field-spec element requires two attributes, "start-pos" which tells where the field starts and "end-pos"
which tells where the field ends.
By using this information, we can define the file spec for our sample-fixed-width-file.dat file, which
looks like the below. Save the following contents into a sample-fixed-width-file-spec.xml file. This can be downloaded
Writing fixed width flat files
Following set of Stpes explains how to generate the sample-fixed-width-file.dat file using file
spec sample-fixed-width-file-spec.xml defined earlier.
- Get the FileWriter by passing the output stream of sample-fixed-width-file.dat file and
input stream of sample-fixed-width-file-spec.xml file. Adjust path references according to your directory structure.
FileWriter fileWriter=FileWriter.getFileWriter(
new FileOutputStream("C:\\sample-fixed-width-file.dat"),
new FileInputStream("C:\\sample-fixed-width-file-spec.xml"));
- Create the writer record from the file writer by telling the type of record you want to create.
Once you get the recrod, write all the field values by using the field name given in the file spec
and submit the records to the file reader to write them into the file.
WriterRecord headerRecord=fileWriter.createWriterRecord(RecordType.HEADER);
headerRecord.writeField("timestamp", "20060812");
fileWriter.writeRecord(headerRecord);
WriterRecord detailRecord=fileWriter.createWriterRecord(RecordType.DETAIL);
detailRecord.writeField("field1", "1234567891");
detailRecord.writeField("field2", "0000012.00");
detailRecord.writeField("field3", "3434343434");
detailRecord.writeField("field4", "7878787878");
fileWriter.writeRecord(detailRecord);
WriterRecord trailerRecord=fileWriter.createWriterRecord(RecordType.TRAILER);
trailerRecord.writeField("recordCount", "0000000.00");
fileWriter.writeRecord(trailerRecord);
- Close the file writer.
fileWriter.close();
For better clarity, all the code is given below.
FileWriter fileWriter=FileWriter.getFileWriter(
new FileOutputStream("C:\\sample-fixed-width-file.dat"),
new FileInputStream("C:\\sample-fixed-width-file-spec.xml"));
WriterRecord headerRecord=fileWriter.createWriterRecord(RecordType.HEADER);
headerRecord.writeField("timestamp", "20060812");
fileWriter.writeRecord(headerRecord);
WriterRecord detailRecord=fileWriter.createWriterRecord(RecordType.DETAIL);
detailRecord.writeField("field1", "1234567891");
detailRecord.writeField("field2", "0000012.00");
detailRecord.writeField("field3", "3434343434");
detailRecord.writeField("field4", "7878787878");
fileWriter.writeRecord(detailRecord);
WriterRecord trailerRecord=fileWriter.createWriterRecord(RecordType.TRAILER);
trailerRecord.writeField("recordCount", "0000000.00");
fileWriter.writeRecord(trailerRecord);
fileReader.close();
Writing XML files
Following set of Stpes explains how to generate the sample-xml-file.xml file using file
spec sample-xml-file-spec.xml defined earlier.
- Get the FileWriter by passing the output stream of sample-xml-file.xml file and
input stream of sample-xml-file-spec.xml file. Adjust path references according to your directory structure.
FileWriter fileWriter=FileWriter.getFileWriter(
new FileOutputStream("C:\\sample-xml-file.xml"),
new FileInputStream("C:\\sample-xml-file-spec.xml"));
- Create the writer record from the file writer by telling the type of record you want to create.
Once you get the recrod, write all the field values by using the field name given in the file spec
and submit the records to the file reader to write them into the file. XMLWriterRecord provides
few more methods to write the nested(complex) elements and repeat elements into the record.
XMLWriterRecord headerRecord=(XMLWriterRecord)fileWriter.createWriterRecord(RecordType.HEADER);
headerRecord.writeSimpleElement("file-type", "Employee Records & Details");
fileWriter.writeRecord(headerRecord);
XMLWriterRecord detailRecord=(XMLWriterRecord)fileWriter.createWriterRecord(RecordType.DETAIL);
detailRecord.writeSimpleElement("first-name", "Suresh");
detailRecord.writeField("last-name", "Pragada");
XMLWriterRecord deptComplexRecord=(XMLWriterRecord)detailRecord.createComplexElement("dept-info");
deptComplexRecord.writeSimpleElement("dept-name", "IT");
deptComplexRecord.writeSimpleElement("dept-location", "LOC1");
fileWriter.writeRecord(detailRecord);
XMLWriterRecord trailerRecord=(XMLWriterRecord)fileWriter.createWriterRecord(RecordType.TRAILER);
trailerRecord.writeSimpleElement("transaction-count", "1");
fileWriter.writeRecord(trailerRecord);
- Close the file writer.
fileWriter.close();
For better clarity, all the code is given below.
FileWriter fileWriter=FileWriter.getFileWriter(
new FileOutputStream("C:\\sample-xml-file.xml"),
new FileInputStream("C:\\sample-xml-file-spec.xml"));
XMLWriterRecord headerRecord=(XMLWriterRecord)fileWriter.createWriterRecord(RecordType.HEADER);
headerRecord.writeSimpleElement("file-type", "Employee Records & Details");
fileWriter.writeRecord(headerRecord);
XMLWriterRecord detailRecord=(XMLWriterRecord)fileWriter.createWriterRecord(RecordType.DETAIL);
detailRecord.writeSimpleElement("first-name", "Suresh");
detailRecord.writeField("last-name", "Pragada");
XMLWriterRecord deptComplexRecord=(XMLWriterRecord)detailRecord.createComplexElement("dept-info");
deptComplexRecord.writeSimpleElement("dept-name", "IT");
deptComplexRecord.writeSimpleElement("dept-location", "LOC1");
fileWriter.writeRecord(detailRecord);
XMLWriterRecord trailerRecord=(XMLWriterRecord)fileWriter.createWriterRecord(RecordType.TRAILER);
trailerRecord.writeSimpleElement("transaction-count", "1");
fileWriter.writeRecord(trailerRecord);
fileReader.close();
Understanding Delimited flat file spec
As mentioned in the Overview section, file spec will be defined in XML format
and tells the structure of the file, information to identify the records and fields
in those records.
All the delimited flat file specs will have the following XML format.
<file-spec file-type="delimited-flat">
<record-spec record-type="DETAIL" delimiter="|" field-count="4">
<field-spec field-name="field-name1" index="1"/>
<-- More field specs will follow -->
</record-spec>
</file-spec>
In this file spec, file-spec element provides the information about the type of the file. For
all the delimited flat files the file-type attribute value should be "delimited-flat".
record-spec element provides the information to identify the record in the file. Delimited
flat file record-spec requires the attributes "record-type", "delimiter" and "field-count".
"record-type" attribute value can be anything that you want to use in your java code,
"delimiter" attribute value tells the parser that all the fields in that record will be delimited
by that value
and "field-count" attribute values tells the number of fields available in the record.
field-spec element requires two attributes, "index" which tells where the field can be found in that
record. Typically, all the delimited file specs will contain only one record spec.
By using this information, we can define the file spec for our sample-delimiter-file.dat file, which
looks like the below. Save the following contents into a sample-delimited-file-spec.xml file. This can be downloaded
Writing delimited flat files
Following set of Stpes explains how to generate the sample-delimited-file.dat file using file
spec sample-delimited-file-spec.xml defined earlier.
- Get the FileWriter by passing the output stream of sample-delimited-file.dat file and
input stream of sample-delimited-file-spec.xml file. Adjust path references according to your directory structure.
FileWriter fileWriter=FileWriter.getFileWriter(
new FileOutputStream("C:\\sample-delimited-file.dat"),
new FileInputStream("C:\\sample-delimited-file-spec.xml"));
- Create the writer record from the file writer by telling the type of record you want to create.
Once you get the recrod, write all the field values by using the field name given in the file spec
and submit the records to the file reader to write them into the file.
WriterRecord detailRecord=fileWriter.createWriterRecord(RecordType.DETAIL);
detailRecord.writeField("field1", "1234567891");
detailRecord.writeField("field2", "0000012.00");
detailRecord.writeField("field3", "3434343434");
detailRecord.writeField("field4", "7878787878");
fileWriter.writeRecord(detailRecord);
- Close the file writer.
fileWriter.close();
For better clarity, all the code is given below.
FileWriter fileWriter=FileWriter.getFileWriter(
new FileOutputStream("C:\\sample_fixed.dat"),
new FileInputStream("C:\\sample-fixed-width-file-spec.xml"));
WriterRecord detailRecord=fileWriter.createWriterRecord(RecordType.DETAIL);
detailRecord.writeField("field1", "1234567891");
detailRecord.writeField("field2", "0000012.00");
detailRecord.writeField("field3", "3434343434");
detailRecord.writeField("field4", "7878787878");
fileWriter.writeRecord(detailRecord);
fileReader.close();