• Post Reply Bookmark Topic Watch Topic
  • New Topic

Appending data to existing data in a file

 
srilatha mahankali
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a req where we read a flat file which contains records line by line and append some additional info to each line when we read it and write the new lines with additional info to a different file. Could somebody pl tell me how do i do this.

Thanks!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What part of it is giving you trouble, exactly? You open one file for reading, and one for writing, and as you read each line from the first, you write it to the second, then write the additional information.
 
srilatha mahankali
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply. May be i wasn't very lear in the above post so, let me explain what i am trying to do here. I have an input file where each line( record) should be of fixed length and all fields are not required so, what they do is if there is no value for a perticular field they usually send spaces. When it comes to the last character in the line it is ignoring all the spaces that are coming after that so, when i check the line length(record) which is a file validation we should do it is not giving the record length that it sould be. For ex let's say that we have a rule like the length of each rec should be 850 when i check the lenght i am getting only 654 or something because it is ignoring all spaces.

Could you pl answer me if there is a way to handle this.

Thanks!
 
Michael Powe
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"it" is not ignoring spaces -- spaces are characters just like another element of text. if you read a line into a String and the length method returns 654, it means there are 654 characters, including spaces. the likelihood is that the tool creating the file truncates trailing spaces -- most decent text editors do this automatically.

there are two approaches to making the String a standard 850 chars. one is to read the string into a StringBuffer and then simply append chars to the required length. this would be quite easy to do, i think.

the other is to insert some boilerplate text like 'n/a' into the empty fields. to do that, you'll have to parse the lines and find the empty fields. the regex package may be a good tool to accomplish this.

make a template out of the required fields, parse the existing line and determine which fields are empty and fill them. of course, that implies that you have a means of distinguishing the contents of the fields, in some way being able to identify which fields have been left blank.

thanks.

mp

Caution: Happy Fun Ball may suddenly accelerate to dangerous speeds.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!