Re: Speeding up reading from files
Hi,
There is two things u can do to improve that code
1. use ByteBuffer and FileChannel
2. instead of adding char by char, create the string in one shot
a sample code can be as below, for a 100M file it reduces the time
from 32 sec to 2 sec on my PC (without the time to add to a vector)
FileInputStream f = new FileInputStream( "fileName" );
int SIZE=1000;
String text;
FileChannel ch = f.getChannel( );
ByteBuffer bb = ByteBuffer.allocateDirect( SIZE );
byte[] barray = new byte[SIZE];
String leftOver="";
int nRead, nGet;
while ( (nRead=ch.read( bb )) != -1 )
{
if ( nRead == 0 )
continue;
bb.position( 0 );
bb.limit( nRead );
while( bb.hasRemaining( ) )
{
nGet =(int) Math.min( bb.remaining( ), SIZE );
bb.get( barray, 0, nGet );
int start=0;
for ( int i=0; i<nGet; i++ ){
if(barray[i]==64 ){ /*ascii value of '@'=64 */
text=leftOver+new String(barray,start,i-start);
start=i+1;
leftOver="";
_msgList.add(text);
// System.out.println(text);
}
}
if(nGet>start){
leftOver=new String(barray,start,nGet-start);
}
}
bb.clear( );
}
if (leftOver.length() > 0) {
// System.out.println("----"+leftOver);
_msgList.add(text);
}
Hope it helps
Saif
On Jan 21, 1:35 pm, bugbear <bugbear@trim_papermule.co.uk_trim> wrote:
Mark wrote:
Hi,
I am using a BufferedReader to read character data in from a file. I=
t
works but it's incredibly slow. (The file consists of a number of
separate messages, each separated by a special character. Each
message must be read into a separate string.)
I use the following code (exception handling removed for brevity):
String text = new String("");
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader=
(new
FileInputStream(_msgFile)));
int c;
while ((c = in.read()) != -1) {
if (c == '@') {
_msgList.add(text);
text = "";
} else {
text += (char)c;
}
}
if (text.length() > 0) {
_msgList.add(text);
}
Try working out (as near as you can) what the line
text += (char)c;
does.
BugBear