大数据量的文件读写 java nio的完全发挥

2017-01-11

小弟不才,自己整理了几种高效的读写大文件的方法,有兴趣的可以看看`package com.nio;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

public class ZeroCopy {

public static void main(String[] args) {
    String frompath=System.getProperty("user.dir") + "\\temp\\xxxx.csv";
    String topath1=System.getProperty("user.dir") + "\\result\\to1.csv";
    String topath2=System.getProperty("user.dir") + "\\result\\to2.csv";
    String topath3=System.getProperty("user.dir") + "\\result\\to3.csv";
    String topath4=System.getProperty("user.dir") + "\\result\\to4.csv";
    ZeroCopy channel = new ZeroCopy();

    try {

        long start1=System.currentTimeMillis();
        channel.zeroCopy(frompath,topath1);
        long cost1=System.currentTimeMillis()-start1;
        System.out.println("zerocopy  cost time:"+cost1);

        long start2=System.currentTimeMillis();
        channel.copy(frompath,topath2);
        long cost2=System.currentTimeMillis()-start2;
        System.out.println("buffer copy  cost time:"+cost2);

        long start3=System.currentTimeMillis();
        channel.channelCopy(frompath,topath3);
        long cost3=System.currentTimeMillis()-start3;
        System.out.println("NIO  cost time:"+cost3);

        long start4=System.currentTimeMillis();
        channel.mapChannel(frompath,topath4);
        long cost4=System.currentTimeMillis()-start4;
        System.out.println("MappedByteBuffer cost time:"+cost4);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void zeroCopy(String from, String to) throws IOException {

    FileChannel source = null;
    FileChannel destination = null;
    try {
        source = new FileInputStream(from).getChannel();
        destination = new FileOutputStream(to).getChannel();
        source.transferTo(0, source.size(), destination);
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

public void copy(String from, String to) throws IOException {
    int buffsize=1*1024;
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(from);
        fos = new FileOutputStream(to,true);
        BufferedReader br=new BufferedReader(new InputStreamReader(fis,"UTF-8"),buffsize);
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"),buffsize);
        int line=0;
        while ((line=br.read())!=-1) {
            bw.write(line);
        }
        br.close();
        bw.close();
        fos.flush();
    } finally {
        if(fis != null) {
            fis.close();
        }
        if(fos != null) {
            fos.close();
        }
    }
}

@SuppressWarnings(“resource”)
public void channelCopy(String from,String to) throws IOException{
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(from).getChannel();
destination = new FileOutputStream(to).getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (source.read(buffer)!=-1){
buffer.flip();
destination.write(buffer);
buffer.clear();
}
} finally {
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
}
@SuppressWarnings(“resource”)
public void mapChannel(String from,String to)throws IOException{
FileChannel source = null;
FileChannel destination = null;
try {
source = new RandomAccessFile(from,”r”).getChannel();
destination = new RandomAccessFile(to,”rw”).getChannel();
long size=source.size();
MappedByteBuffer readbuff =source.map(MapMode.READ_ONLY,0,size);
MappedByteBuffer writebuff =destination.map(MapMode.READ_WRITE,0,size);
byte[] buffer=new byte[1024];
int index=readbuff.remaining();
while (true) {
if(index<1024){
readbuff.get(buffer,0,index);
writebuff.put(buffer,0,index);
break;
}else {
readbuff.get(buffer);
writebuff.put(buffer);
index-=1024;
}
}
writebuff.force();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
}
}
`
里面注释比较少,在银行上班呢,所以各种权限很扯,输入法都是系统自带,所以不乡想打太多的英文请见凉。测试了30M的文件。所用时间如下:这里写图片描述

顶部