import java.io.File;
import java.io.FileInputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
/*
* Compare two files byte by byte effectively
* using new java.nio classes
*/
public class CompareFiles {
static int compare(String fname1, String fname2) throws Exception {
return getMBB(fname1).compareTo(getMBB(fname2));
}
// getMappedByteBuffer
private static MappedByteBuffer getMBB(String fname) throws Exception {
File file=new File(fname);
return new FileInputStream(file).getChannel().
map(MapMode.READ_ONLY,0L,file.length());
}
public static void main(String args[]) throws Exception {
int ret=0;
System.out.println("compare('"+args[0]+ "', '" + args[1] + "') = ");
long start=System.currentTimeMillis();
System.out.println( (ret=compare(args[0], args[1])) +
"\n in "+(System.currentTimeMillis()-start)+" ms");
System.exit(ret);
}
} //end of public class CompareFiles
=1=
THE END |