What is the best way of parsing a flat file with fixed positions in Java

certs33
certs33 used Ask the Experts™
on
What I want to do is parsing a flat file (attached) with fixed positions. You see there six different rows. Each row is identified with starting character. I will store the fields in the memory after parsing this file. According to the data stored in Oracle database table will modify these parsed fields and recreate the text file with modified fields?
Any approach, example code will be appreciated.
sample.txt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
Difficult to say exactly without knowing exactly the file spec

Commented:
Using the Oracle DB, you can read the flat files thanks to UTL_FILE amd creating a PL/SQL procedure you coud read each line and feed an appropriate destination table. To modify the data on the fly, on can do the tranformation in the same PL/SQL procedure or use triggers on the destination table.
Hope it helps.
Assuming the file has fixed-size fields, try this way:
create an array of integer, which represent the ordered list of each field length, then you could cycle through this array and get a substring of each row content using the array values, using them as index/length for the substring.
This way you could build an array of strings which stores the values in memory.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Top Expert 2016

Commented:
It's unclear (apparently not just to me) whether you want to parse the file in Java or Oracle. Could you clarify please certs33?

Author

Commented:
I will try to clarify some points:
1. The length of every row in the file is fixed (200 characters)
2. Every row has fixed-size fields. As an example the the row starting with 3 has about 60 fields with different sizes.
3. I have data in an Oracle table. This data will be used for transformation of the file. According to the data in the table some of these 159 fields will be changed. The rows starting with 4 are dependent to the row starting with 3 which is just above. The change on the rows starting with 3 will cause some changes for the rows starting with 4. (some may be removed, new ones may be created)
4. A new flat file will be created with new rows
The target is doing all the steps (parse,modify,recreate the file) in Java not PL/SQL. This file can be huge (about 1 GB). I need an approach for starting. Is using substring function the best way for parsing?
           
Top Expert 2016
Commented:
>>This file can be huge (about 1 GB). I need an approach for starting. Is using substring function the best way for parsing?

Personally i would use nio techniques for doing this. It will probably be the most efficient approach

>>As an example the the row starting with 3 has about 60 fields with different sizes.

Of course, this kind of info is crucial

Author

Commented:
CEHJ have you any example fro nio?
This is a schedule sheet, much like you get from oag or sita or etc.
theres a perl library to parse this.
In java, Nope, but easy to create, I made one in 2004.
I my memory is still well, I can help you describe the fields.
Top Expert 2016

Commented:
>>CEHJ have you any example fro nio?

A little busy at the moment i'm afraid. If you haven't found anything later you could try contacting me through my profile

Author

Commented:
guneshraj, can you please provide more information about perl library. Your java experience will also be appreciated.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial