码迷,mamicode.com
首页 > 其他好文 > 详细

nio通道通信实现的文件复制

时间:2018-09-12 13:55:13      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:get   通道   color   main   pat   public   div   map   ann   

 1 package edzy.nio;
 2 
 3 import org.junit.Test;
 4 
 5 import java.io.FileInputStream;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.nio.ByteBuffer;
 9 import java.nio.MappedByteBuffer;
10 import java.nio.channels.FileChannel;
11 import java.nio.file.Paths;
12 import java.nio.file.StandardOpenOption;
13 
14 public class Channel {
15  @Test
16     public void transfer(){
17         FileChannel in = null;
18         FileChannel out = null;
19         try {
20             in = FileChannel.open(Paths.get("./src/main/java/edzy/nio/s.jpg"),StandardOpenOption.READ);
21             out = FileChannel.open(Paths.get("./src/main/java/edzy/nio/s5.jpg"),StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE_NEW);
22             //nio通道通信
         out.transferFrom(in,0,in.size()); 23 24 } catch (IOException e) { 25 e.printStackTrace(); 26 }finally { 27 if(out != null){ 28 try { 29 out.close(); 30 } catch (IOException e) { 31 e.printStackTrace(); 32 } 33 } 34 35 if(in != null){ 36 try { 37 in.close(); 38 } catch (IOException e) { 39 e.printStackTrace(); 40 } 41 } 42 43 44 } 45 } 46 47 48 }

 

nio通道通信实现的文件复制

标签:get   通道   color   main   pat   public   div   map   ann   

原文地址:https://www.cnblogs.com/kill-9/p/9634215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!