Is it possible to change ONE line in a txt file WITHOUT reading and writing the whole file (Java)? -


say have text file:

i love bananas. <age> 23 </age> years old. beaches. 

all want open said file , change age 24 or 25. way in java without reading writing whole file? know possible buffered reader , writer, looking less memory intensive way when file gets huge.

short answer: no.

long answer: not really. if know text you're inserting directly replaces text you're removing perform substitution seeking portion of file, reading small block, substituting new text , replacing block. work your age replacement example, not if you're replacing 9 10, or 99 100, example.

however, there other difficulties:

you may have variable length lines, don't know seek to. still have read file far place make substitution, if block @ time , discard unwanted ones.

you have further complexity multi-byte character sets utf-8. ingoing text may display same length outgoing text, may shorter or longer depending on substitution.

e (u+0065) codes 1 byte, é (u+00e9) codes two.

but, if you're looking less memory intensive method can read file 1 line @ time, , write each line new file process it. when you're done, delete old file , rename new one. might slow, buffering reads , writes to, say, 20 lines (or 100, or 1000, or whatever choose..) improve performance.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -