nio filechannel

来源:互联网 发布:mac魔兽世界设置 编辑:程序博客网 时间:2024/05/24 02:33

nio fileChannel使用:

 try(FileInputStream in=new FileInputStream("D:/soft/Evernote_6.5.4.4720.exe");            FileOutputStream out=new FileOutputStream("E:/Evernote_6.5.4.4720.exe");            FileChannel fcin=in.getChannel();            FileChannel fcou=out.getChannel()) {                       fcou.transferFrom(fcin,0,fcin.size());   //使用transferFrom 直接将 fcin的数据输出到fc      }


 try(FileInputStream in=new FileInputStream("D:/soft/Evernote_6.5.4.4720.exe");            FileOutputStream out=new FileOutputStream("E:/Evernote_6.5.4.4720.exe");            FileChannel fcin=in.getChannel();            FileChannel fcou=out.getChannel()) {                       fcin.transferTo(0,fcin.size(),fcou);  //使用transferTo 直接将 fcin的数据输出到fc  }


使用ByteBuffer写法:

try(FileInputStream in=new FileInputStream("D:/soft/Evernote_6.5.4.4720.exe");            FileOutputStream out=new FileOutputStream("E:/Evernote_6.5.4.4720.exe");            FileChannel fcin=in.getChannel();            FileChannel fcou=out.getChannel()) {            ByteBuffer bf=ByteBuffer.allocate(1024);                            while(fcin.read(bf) != -1) {                bf.flip();                while(bf.hasRemaining()){                    fcou.write(bf);                }                bf.clear();            } }


原创粉丝点击