• Post Reply Bookmark Topic Watch Topic
  • New Topic

Text file parsing  RSS feed

 
Christina Shah
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.

I have a huge text file with strings with : delimited values as -

1000:2000:454758:676869:bad:bad:546474:... and so on

I want to pick up (let's say) the 8th and 12th values and replace them with a string if they are not valid numbers. What is the most efficent way? This is a large file, I want to do it most optimized.

I can use split(regex, n).

TIA
 
Kevin Mangold
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the RandomAccessFile class. You can open a file for reading and writing, read until you get to the point where you need, then you can output your changes, all from within the same class. I think this would be the best and easiest way of doing what you're describing.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would not use a RandomAccessFile for this unless the replacement string is guaranteed to be exaxtly the same length in bytes as the number it's replacing. Otherwise you would need to spend time shifting the position of all the subsequent bytes to the left or right. Which would be very inefficient.

What I would do: read each line of the file using a Scanner (or, prior to JDK 5, a BufferedReader). As you do, write the contents to a new file using a PrintWriter. Within each line, count off the positions of the ';' characters using String's indexOf() method. Using split() would work too, but I think indexOf() will be faster here. I could be wrong, or it may not really matter, so do whichever suits you, and see how fast it is.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!