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.
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.
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.
"I'm not back." - Bill Harding, Twister
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad: