java的两种文件拷贝方式

一.基础

三种IO方式

1.传统IO方式,基于流模型实现,提供了File抽象,输入输出流等,交互方式是同步,阻塞的方式,也就是说在读写动作完成之前,线程一直阻塞在那里。

2.NIO  引入了Channel,Selector,Buffer等新的抽象,可以构建多路复用的,同步非阻塞的IO程序

3.AIO  异步非阻塞

二.实现

1.利用java.io.类库,直接为源文件构建一个FileInputStream读取,然后再为目标文件构建一个FileOutputStream,完成写入工作

public  static  boolean  copyFile(String  source,String  dest){
    InputStream  inputStream = null;
    OutputStream outputStream = null;
    try {
        inputStream = new FileInputStream(source);
        outputStream = new FileOutputStream(dest);
        byte[]  buffer = new byte[1024];
        int bytesRead;
        while((bytesRead=inputStream.read(buffer))!=-1){
            outputStream.write(buffer, 0, bytesRead);
        }
        inputStream.close();
        outputStream.close();
    } catch (FileNotFoundException e) {
        logger.warn("fileUtil_copyFile_sourceFile",source,e);
        return false;
    } catch (IOException e) {
        logger.warn("fileUtil_copyFile_param{}_{}",source,dest,e);
        return false;
    }
    return true;
}

2.利用java.nio类库提供的transferTo或transferFrom方式实现

public  static  boolean  copyFileNio(String  source,String  dest){
    FileInputStream  inputStream = null;
    FileOutputStream outputStream = null;
    try {
        inputStream = new FileInputStream(source);
        outputStream = new FileOutputStream(dest);
        FileChannel sourceCh = inputStream.getChannel();
        FileChannel destCh = outputStream.getChannel();
        destCh.transferFrom(sourceCh, 0, sourceCh.size());
        sourceCh.close();
        destCh.close();
    } catch (FileNotFoundException e) {
        logger.warn("fileUtil_copyFile_sourceFile",source,e);
        return false;
    } catch (IOException e) {
        logger.warn("fileUtil_copyFile_param{}_{}",source,dest,e);
        return false;
    }
    return true;
}

经过测试第二种方式要比第一种方式快很多

第一种方式:

     当使用输入输出流进行读写时,实际上进行了多次上下文切换,比如应用读取数据时,先在内核态将数据从磁盘读取到内核缓存,在切换到用户态将数据从内核缓存读取到用户缓存。

第二种方式:

    实现了零拷贝,数据传输并不需要用户态参与,省去上下文切换的开销和不必要的内存拷贝,进而可能提高应用拷贝性能。注意,transferTo 不仅仅是可以用在文件拷贝中,与其类似的,例如读取磁盘文件,然后进行 Socket 发送,同样可以享受这种机制带来的性能和扩展性提高。 

猜你喜欢

转载自blog.csdn.net/oracle1158/article/details/84768678