最近在学习java NIO,然后写了三个程序来测试,分别为传统的IO加上缓冲区,直接缓冲区,和非直接缓冲区三种方式,测试的结果显示为直接缓冲区最快,传统IO加上缓冲区第二,最后是NIO非直接缓冲区。
package com.wmx.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
/**
* 通道的主要实现类 java.nio.channels.Channel FileChannel 本地的通道 网络的通道 SocketChannel
* ServerSocketChannel DatagramChannel
*
*
* @author Shinelon
*
*/
public class TestChannel {
//共耗时1017毫秒
//通道之间的数据传输
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
FileChannel inChannel=null;
FileChannel outChannel=null;
try {
inChannel = FileChannel.open(Paths.get("E:/BaiduNetdiskDownload/Microsoft Office 2013(64位).iso"),StandardOpenOption.READ);
outChannel = FileChannel.open(Paths.get("E:/BaiduNetdiskDownload/1.jpg"),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE_NEW);
// inChannel.transferTo(0, inChannel.size(), outChannel);
outChannel.transferFrom(inChannel, 0, inChannel.size());
} catch (IOException e) {
e.printStackTrace();
}finally{
if(inChannel!=null){
try {
inChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outChannel!=null){
try {
outChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("共耗时"+(endTime-startTime)+"毫秒");
}
public static void main3(String[] args) {
//共耗时2728毫秒
long startTime = System.currentTimeMillis();
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
fis = new FileInputStream(new File("E:/BaiduNetdiskDownload/Microsoft Office 2013(64位).iso"));
fos = new FileOutputStream(new File("E:/BaiduNetdiskDownload/1.jpg"));
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] by = new byte[1024];
int len = 0;
while ((len = bis.read(by)) != -1) {
bos.write(by);
bos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("共耗时" + (endTime - startTime) + "毫秒");
}
// 使用直接缓冲区来完成文件的复制
public static void main(String[] args) {
// 总耗时1512毫秒 提高两倍 总耗时1093毫秒
long startTime = System.currentTimeMillis();
FileChannel inChannel = null;
FileChannel outChannel = null;
MappedByteBuffer inMapperChannel;
MappedByteBuffer outMapperChannel;
try {
inChannel = FileChannel.open(Paths.get("E:/BaiduNetdiskDownload/Microsoft Office 2013(64位).iso"),
StandardOpenOption.READ);
outChannel = FileChannel.open(Paths.get("E:/BaiduNetdiskDownload/4.jpg"), StandardOpenOption.WRITE,
StandardOpenOption.READ, StandardOpenOption.CREATE_NEW);
// 原理和原来allocate
// 内存映射文件
inMapperChannel = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size());
outMapperChannel = outChannel.map(MapMode.READ_WRITE, 0, inChannel.size());
// 直接对缓冲区进行数据的读写操作
byte[] by = new byte[inMapperChannel.limit()];
inMapperChannel.get(by);
outMapperChannel.put(by);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outChannel != null) {
try {
outChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inChannel != null) {
try {
inChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("总耗时" + (endTime - startTime) + "毫秒");
}
// 非直接缓冲区
public static void main1(String[] args) {
// 总耗时3630毫秒 总耗时3592毫秒 总耗时3751毫秒
long startTime = System.currentTimeMillis();
FileInputStream fis = null;
FileOutputStream fos = null;
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
// 1.利用通道来完成文件的复制
fis = new FileInputStream("E:/BaiduNetdiskDownload/Microsoft Office 2013(64位).iso");
fos = new FileOutputStream("E:/BaiduNetdiskDownload/5.jpg");
// 获取通道
inChannel = fis.getChannel();
outChannel = fos.getChannel();
// 分配制定大小的缓冲区
ByteBuffer buf = ByteBuffer.allocate(1024);
// 将通道中的数据存入到缓冲区
while (inChannel.read(buf) != -1) {
buf.flip();// 切换到读取数据模式
// 将数据写入到通道
outChannel.write(buf);
buf.clear();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outChannel != null) {
try {
outChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inChannel != null) {
try {
inChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("总耗时" + (endTime - startTime) + "毫秒");
}
}