I/O stream for reading and editing a file full of numbers
I have a file with a lot of numbers in it and I want to read the numbers from the file one by one, test them and then either leave them or delete them from the file. What I/O stream would be good for that? It would need to be able to remove the number without leaving whitespace, for example from 1 2 3 4 5 to 1 3 5.
Re: I/O stream for reading and editing a file full of numbers
Read the file and parse as desired into a StringBuilder/Buffer, then over-write the file with the new data. You could wrap a FileInputStream and FileOutputStream in a BufferedReader/BufferedWriter to do the reading/writing, respectively.
Re: I/O stream for reading and editing a file full of numbers
Thanks, but that's not quite what I want. I intend for the file to have a few million numbers in it so reading and overwriting everything every time would take a bit long. What I'm looking for should be able to read a number, mark the position of the first digit and then erase the bit of file from that position to the end of the number (like write an empty string for every digit in the number or something).
Is something like this possible?
Re: I/O stream for reading and editing a file full of numbers
Quote:
I intend for the file to have a few million numbers in it so reading and overwriting everything every time would take a bit long.
Not necessarily. Did you try it? You can add a slight twist to the method I mentioned if you're files are huge and you worry about reading it all into memory...as you read the first file, parse it line by line, and write these lines out to a temp file. When complete, delete the first and rename the second the that of the first.