I was under the impression that using FileChannel and BytBuffer would speed the read time but it seems to be significantly slower than reading from a filestream. Am I doing something wrong here?
FileInputStream fis = new FileInputStream("C:\\Users\\blah\\Desktop\\del\\pg28054.txt");
FileOutputStream fos = new FileOutputStream("C:\\Users\\blah\\Desktop\\del\\readme.txt");
FileChannel fcin = fis.getChannel();
FileChannel fcout = fos.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
long startTime = System.currentTimeMillis();
long endtime = System.currentTimeMillis();
while(true){
buffer.clear();
int r = fcin.read(buffer);
if(r==-1){
break;
}
buffer.flip();
fcout.write(buffer);
}
endtime = System.currentTimeMillis();
System.out.println("time to read and write(ms) " + (endtime - startTime));
The above completes in 108 ms where are the below implementation does it in 43 ms
long startTime;
long endtime;
FileInputStream fis1 = new FileInputStream("C:\\Users\\blah\\Desktop\\del\\pg28054.txt");
FileOutputStream fos1 = new FileOutputStream("C:\\Users\\blah\\Desktop\\del\\readme1.txt");
byte b[] = null;
startTime = System.currentTimeMillis();
while(true){
b = new byte[1024];
int r = fis1.read(b);
if(r==-1){
break;
}
fos1.write(b);
}
endtime = System.currentTimeMillis();
System.out.println("time to read and write(ms) " + (endtime - startTime));