• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Text file parsing

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It's never done THAT before. Explain it to me tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic