Forums Register Login

Text file parsing

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1208 times.
Similar Threads
reading a file column wise
How to Add the Text field to the TextArea or TextPane
how to string search ?
servlets and text file data
Log4J Configuration
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:00:31.