Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

I/O based on record layout  RSS feed

 
harsha puttaswamy
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to Java IO API and I am looking to read and write records based on a fixed record layout. Can somebody please point me in the right direction ?

The data files are plain text files with one record per line.

Thanks,
Harsha.
An afternoon to learn - a lifetime to master
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you truly mean fixed-size record, then RandomAccessFile will be very helpful. If not, and it's only that each variable-size record is delimited by a newline, then BufferedReader's readLine() will work, but you won't be able to easily skip ahead to a particular record.
 
harsha puttaswamy
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David. The records are fixed length. Also, if I have to parse the record that I read based on positions, will RandomAccessFile support that ? For Ex: Say on the record, positions 1 thru 9 contains ssn, 10-30 contains last name and 31-51 contains first name. If I read this record, what would I use in order to parse at the correct positions, that will give the scalar values ?

Thanks for your help.
Harsha.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: I'm assuming the indices you have used (e.g. 1-9) are one-based. You'll need to convert these to zero-based indices (e.g. 0-8) to match the classes you'll be using.

First, use seek(long) to move the file pointer to the record you want to read, unless you're reading them sequentially. Next, use readFully(byte[]) to read in the fields as byte arrays and then convert them to Strings. I have no idea what will happen with zero-padded byte arrays, if that's how they appear in the file.

For example, to store the first name "Harsha" do you pad the remaing fifteen bytes with zeros or spaces? You'll have to try it out and see what you get.This won't perform very well, and looking at the other String constructors I already see a way to speed things up while somewhat obscuring the logic. String(byte[], offset, length) will create a String from a portion of the array.If you're on JDK 1.4 or later, I would bet that NIO provides much better methods for achieving the same goals. It's buffer-oriented which seems well-suited to fixed-length records, and it has the ability to back a character buffer with a byte buffer, performing the conversions more naturally.

If you want to go that route, check out Sun's and IBM's NIO tutorials. Try searching on Merlin too as that was the codename.
 
harsha puttaswamy
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David. Will try and see what happens.

Harsha.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!