1. 程式人生 > >nio通道通信實現的文件復制

nio通道通信實現的文件復制

exce cat org junit from chan ava pac nts

 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通道通信實現的文件復制