使用NIO的FileChannel和ByteBuffer高效读取文件
2012-03-30 09:47
459 查看
http://hi.baidu.com/boywell/blog/item/658d143f730a553770cf6ccd.html
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.channels.FileChannel;
public class ReadWriteCompare
{
public static void main(String[] args) throws IOException
{
FileInputStream fileInputStream = new FileInputStream("f:"+ File.separator +"IBM e-Mentor Program Kickoff Night 1105.pdf");
FileOutputStream fileOutputStream = new FileOutputStream("f:" + File.separator + "test.pdf");
FileChannel inChannel = fileInputStream.getChannel();
FileChannel outChannel= fileOutputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
//Direct Buffer的效率会更高。
// ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
long start = System.currentTimeMillis();
while(true)
{
int eof = inChannel.read(byteBuffer);
if(eof == -1 ) break;
byteBuffer.flip();
outChannel.write(byteBuffer);
byteBuffer.clear();
}
System.out.println("spending : " + (System.currentTimeMillis()-start));
inChannel.close();
outChannel.close();
}
}
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.channels.FileChannel;
public class ReadWriteCompare
{
public static void main(String[] args) throws IOException
{
FileInputStream fileInputStream = new FileInputStream("f:"+ File.separator +"IBM e-Mentor Program Kickoff Night 1105.pdf");
FileOutputStream fileOutputStream = new FileOutputStream("f:" + File.separator + "test.pdf");
FileChannel inChannel = fileInputStream.getChannel();
FileChannel outChannel= fileOutputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
//Direct Buffer的效率会更高。
// ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
long start = System.currentTimeMillis();
while(true)
{
int eof = inChannel.read(byteBuffer);
if(eof == -1 ) break;
byteBuffer.flip();
outChannel.write(byteBuffer);
byteBuffer.clear();
}
System.out.println("spending : " + (System.currentTimeMillis()-start));
inChannel.close();
outChannel.close();
}
}
相关文章推荐
- java nio 之MappedByteBuffer,高效文件/内存映射
- Java NIO之MappedByteBuffer,高效文件内存映射
- 【NIO】通过FileChannel读取包含中文内容的文件
- FileChannel、ByteBuffer对文件操作过程对比
- 【JavaNIO的深入研究4】内存映射文件I/O,大文件读写操作,Java nio之MappedByteBuffer,高效文件/内存映射
- Java使用ByteBuffer读取大文件
- NIO学习:使用Channel、Buffer写入文件
- java nio 之MappedByteBuffer,高效文件/内存映射
- 【JavaNIO的深入研究4】内存映射文件I/O,大文件读写操作,Java nio之MappedByteBuffer,高效文件/内存映射
- 使用MappedByteBuffer(映射文件)对excel表格进行随机定位和读取
- 使用MappedByteBuffer读取大文件(1G以上)和释放MappedByteBuffer的资源
- NIO ByteBuffer 使用方法
- RandomAcessFile、MappedByteBuffer和缓冲读/写文件
- RandomAcessFile、MappedByteBuffer和缓冲读/写文件
- 使用byteBuffer的 position和limit方法分段读取字节流
- 使用FileReader接口读取文件内容
- Java基础之读文件——使用通道随机读取文件(RandomFileRead)
- 使用FileReader和FileWriter读取写入文件内容
- 使用FileReader读取本地磁盘文件问题
- PHP使用fopen与file_get_contents读取文件实例分享