Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace first line of a text file in Java

Tags:

java

I have a text file where I want to change only the first line of the file. The file could be millions of rows long, so I'd rather not have to loop over everything, so I'm wondering if there is another way to do this.

I'd also like to apply some rules to the first line so that I replace instances of certain words with other words.

Is this possible?

like image 305
Bialecki Avatar asked Oct 14 '08 17:10

Bialecki


1 Answers

You want a RandomAccesssFile. Using the file you can read and write wherever you want in the file.

It is much like an InputStream and OutputStream, but it allows reading and writing wherever you like.

like image 85
jjnguy Avatar answered Oct 29 '22 13:10

jjnguy