java.nio.ByteBuffer类的allocateDirect() 方法用来分配直接的内存缓冲区,新分配的内存会被初始化为全0。
这个方法的效率会比allocate()方法的效率高出25%-75%。
方法定义:
public static ByteBuffer allocateDirect(int capacity)
参数: capacity a单位是字节,即capacity个字节.
返回值: 返回新创建的byte缓冲区。
Exception: 如果传入的参数是负数,会抛出 IllegalArgumentException 异常。
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class FastCopyFile
{
static public void main( String args[] ) throws Exception {
if (args.length<2) {
System.err.println( "Usage: java FastCopyFile infile outfile" );
System.exit( 1 );
}
String infile = args[0];
String outfile = args[1];
FileInputStream fin = new FileInputStream( infile );
FileOutputStream fout = new FileOutputStream(