NIO的优势在网络编程,文件处理这块了解就可以了:FileChannel只能工作在阻塞模式下,只有那些和网络相关的channel才能以非阻塞模式工作
一、基本使用
其实还可以用transferFrom()方法或者transferTo()方法,底层使用了零拷贝进行优化,比传统的io流高效
实例:将的内容复制到中
publicstaticvoidfileChannelTest1(){try(RandomAccessFilefile=newRandomAccessFile("E:\\","rw");RandomAccessFilefile2=newRandomAccessFile("E:\\","rw")){FileChannelchannel1=();FileChannelchannel2=();longposition=0;longcount=();(channel1,position,count);}catch(IOExceptione){();}}
运行效果如下
transferFrom()和transferTo()差不多
publicstaticvoidfileChannelTest1(){try(RandomAccessFilefile=newRandomAccessFile("E:\\","rw");RandomAccessFilefile2=newRandomAccessFile("E:\\","rw")){FileChannelchannel1=();FileChannelchannel2=();longposition=0;longcount=();(position,count,channel2);}catch(IOExceptione){();}}
二、transferTo方法传输大于2G的文件
版权声明:本站所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流,不声明或保证其内容的正确性,如发现本站有涉嫌抄袭侵权/违法违规的内容。请举报,一经查实,本站将立刻删除。