Skip parts while reading and writing a file in Android/Java
<p>I'm trying to learn Java/Android and right now I'm doing some experiments with the <code>replaceAll</code> function. But I've found that with large text files the process gets sluggish so I was wondering if there is a way to skip the "useless" parts of a file to have a better performance. (Note: Just skip them, not delete them)</p>

<p>Note: I am not trying to "count lines" or "println" or "system.out", I'm just replacing strings and saving the changes in the same file.</p>

<p><b><em>Example</em></b></p>

<p><b>AAAA</b></p>

<p><b>CCCC-</b> 9234802394819102948102948104981209381'238901'2309'129831'2381'2381'23081'23081'284091824098304982390482304981'20841'948023984129048'1489039842039481'204891'29031'923481290381'20391'294872385710239841'20391'20931'20853029573098341'290831'20893'12894093274019799919208310293810293810293810293810298'120931¿2093¿12039¿120931¿203912¿0391¿203912¿039¿12093¿12093¿12093¿12093¿12093¿1209312¿0390¿... <b>DDDD</b></p>

<p><b>AAAA</b></p>

<p><b>CCCC-</b> 9234802394819102948102948104981209381'238901'2309'129831'2381'2381'23081'23081'284091824098304982390482304981'20841'948023984129048'1489039842039481'204891'29031'923481290381'20391'294872385710239841'20391'20931'20853029573098341'290831'20893'12894093274019799919208310293810293810293810293810298'120931¿2093¿12039¿120931¿203912¿0391¿203912¿039¿12093¿12093¿12093¿12093¿12093¿1209312¿0390¿... <b>DDDD</b></p>

<p><b>and so on....like a zillion times</b></p>

<p>I want to replace all <b>"AAAA"</b> with <b>"BBBB"</b>, but there are large portions of data between the strings I am replacing. Also, this portions always begin with <b>"CCCC"</b> and end with <b>"DDDD"</b>.</p>

<p>Here's the code I am using to replace the string.</p>

<pre><code>File file = new File("my_file.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "", oldtext = "";
while((line = reader.readLine()) != null) {
   oldtext += line + "\r\n";
}
reader.close();

// Replacing "AAAA" strings
String newtext= oldtext.replaceAll("AAAA", "BBBB");

FileWriter writer = new FileWriter("my_file.txt");
writer.write(newtext);
writer.close();
</code></pre>

<p>I think reading all lines is inefficient, especially when you won't be modifying these parts (and they represent the 90% of the file).</p>

<p>Does anyone know a solution???</p>
