NIO通道(channel)原理与获取
2017-09-06 00:00
423 查看
一、通道(Channel): 用于源节点与目标节点的连接。在java NIO中负责缓冲区中数据的传输。Channel本身不存储数据,因此需要配合缓冲区进行传输。
二、通道的主要实现类
java.nio.channels.Channel接口:
|– FileChannel
|– SocketChannel
|– ServerSocketChannel
|– DatagramChannel
三、获取通道
1、java针对支持通道的类提供了getChannel()方法
本地IO:
FileInputStream/FileOutputStream
RandomAccessFile
网络IO:
Socket
ServerSocket
DatagramSocket
2、在jdk1.7中NIO.2针对各个通道提供了静态方法open()
3、在jdk1.7中NIO.2的Files工具类的newByteChannel()
四、通道之间的数据传输
二、通道的主要实现类
java.nio.channels.Channel接口:
|– FileChannel
|– SocketChannel
|– ServerSocketChannel
|– DatagramChannel
三、获取通道
1、java针对支持通道的类提供了getChannel()方法
本地IO:
FileInputStream/FileOutputStream
RandomAccessFile
网络IO:
Socket
ServerSocket
DatagramSocket
2、在jdk1.7中NIO.2针对各个通道提供了静态方法open()
3、在jdk1.7中NIO.2的Files工具类的newByteChannel()
@Test //利用通道完成文件复制 public void test4() throws FileNotFoundException{ FileInputStream fis = new FileInputStream("1.mp4"); FileOutputStream fos=new FileOutputStream("2.mp4"); //1、获取通道 FileChannel inChannel = fis.getChannel(); FileChannel outChannel = fos.getChannel(); try { //2、分配一个指定大小的缓冲区 ByteBuffer buf=ByteBuffer.allocate(1024); //3、将通道中的数据存入缓冲区 while(inChannel.read(buf)!=-1){ //4、将缓冲区中的数据写入通道中 buf.flip(); //切换读取数据模式 outChannel.write(buf); buf.clear(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { outChannel.close(); } catch (Exception e) { e.printStackTrace(); } try { inChannel.close(); } catch (IOException e) { e.printStackTrace(); } try { fos.close(); } catch (IOException e) { e.printStackTrace(); } try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } @Test //使用直接缓冲区完成文件的复制 public void test5() throws IOException{ FileChannel inChannel=FileChannel.open(Paths.get("1.mp4"),StandardOpenOption.READ); FileChannel outChannel=FileChannel.open(Paths.get("2.mp4"),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE_NEW); //内存映射文件 MappedByteBuffer inMappedBuf=inChannel.map(MapMode.READ_ONLY,0,inChannel.size()); MappedByteBuffer outMappedBuf=outChannel.map(MapMode.READ_WRITE,0,inChannel.size()); //直接对缓冲区进行数据的读写操作 byte[] dst=new byte[inMappedBuf.limit()]; inMappedBuf.get(dst); outMappedBuf.put(dst); outChannel.close(); inChannel.close(); }
四、通道之间的数据传输
@Test // 通道之间的数据传输(直接缓冲区) public void test6() throws IOException{ FileChannel inChannel=FileChannel.open(Paths.get("1.mp4"),StandardOpenOption.READ); File 7fe0 Channel outChannel=FileChannel.open(Paths.get("2.mp4"),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE); //inChannel.transferTo(0,inChannel.size(),outChannel); //二选一即可 outChannel.transferFrom(inChannel,0,inChannel.size()); outChannel.close(); inChannel.close(); }
相关文章推荐
- Java-NIO(四):通道(Channel)的原理与获取
- JAVA NIO 通道Channel的原理与获取方法
- NIO通道(channel)原理与获取
- Java-NIO(四):通道(Channel)的原理与获取
- Java Nio 十二、Java NIO DatagramChannel(数据报通道)
- 关于NIO笔记(三):通道(Channel)
- NIO学习笔记通道Channel
- Java的NIO之Channel通道
- nio再学习之通道channel
- NIO理解通道 selector SeverSocketChannel serverSocket selectionKey
- Java NIO Channel & Buffer(Java NIO 通道和缓存)
- Java nio 学习笔记(一) Buffer(缓冲区)与Channel(通道)的相关知识
- Java NIO深入理解Channel(通道)
- NIO channel Socket通道
- Java基础知识强化之IO流笔记76:NIO之 Channel(通道)之间的数据传输
- java.nio.channels.FileChannel文件通道源码解析
- Java NIO系列教程(二) Channel原文链接 作者:Jakob Jenkov 译者:airu 校对:丁一 Java NIO的通道类似流,但又有些不同: 既可以从通
- Java的NIO之Channel通道
- NIO 之 Channel实现原理
- 08. Java NIO FileChannel 文件通道