您的位置:首页 > 编程语言 > Java开发

Java NIO中的通道Channel

2017-08-27 14:42 495 查看
一、通道的概念:

独立的处理器(专门用于IO操作)

 

1
* 一、用于源节点和目标节点的连接,负责缓冲区中数据的传输,不负责存储,需配合缓冲区进行操作。
2
*
3
*二、通道类:
4
*java.nio.channel包下
5
*FileChannel(文件IO)
6
*SocketChannel(TCP的IO套接字)
7
*ServerSocketChannel(TCP的IO套接字)
8
*DatagramChannel(UDP的IO套接字)
9
*
10
*三、获取通道
11
*-- 1.getChannel方法
12
*         FileInputStream/FileOutputStream/RandomAccessFile
13
*         Socket/ServerSocket/Datagram
14
*
15
*-- 2.JDK1.7 中的 NIO.2 的静态方法---open()
16
*
17
*-- 3.JDK1.7 中的 NIO.2 的 Files工具类的 newByteChannel() 方法
二、代码实现:



1

//非直接缓冲区方式完成通道复制文件(allocate方法)


2

public void test1() throws IOException {


3

       //生成文件流


4

       FileInputStream fis = new FileInputStream("D://a.txt");


5

       FileOutputStream fos = new FileOutputStream("D://b.txt");


6

       //生成缓冲区


7

       ByteBuffer byteBuffer = ByteBuffer.allocate(1024);


8

       //生成通道


9

       FileChannel inChannel = fis.getChannel();


10

       FileChannel outChannel = fos.getChannel();


11

       //将通道中的数据读入缓冲区


12

       while (inChannel.read(byteBuffer) != -1) {


13

           //切换成读模式


14

           byteBuffer.flip();


15

           //将缓冲区的数据写入到通道


16

           outChannel.write(byteBuffer);


17

           byteBuffer.clear();


18

      }


19

       outChannel.close();;


20

       inChannel.close();


21

       fis.close();


22

       fos.close();


23

}


24



25



26

   //直接缓冲区方式完成通道复制文件(使用内存映射文件)


27

   @Test


28

   public void test2()throws Exception{


29

       //使用直接缓冲区获取通道


30

       FileChannel inChannel = FileChannel.open(Paths.get("D://a.txt"), StandardOpenOption.READ);


31

       FileChannel outChannel = FileChannel.open(Paths.get("D://b.txt"), StandardOpenOption.WRITE, StandardOpenOption.READ);


32



33

       //获取内存映射文件


34

       MappedByteBuffer inMappedByteBuffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());


35

       MappedByteBuffer outMappedByteBuffer = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());


36



37

       //直接对缓冲区进行数据读写操作


38

       byte[] dst = new byte[inMappedByteBuffer.limit()];


39

       inMappedByteBuffer.get(dst);


40

       outMappedByteBuffer.put(dst);


41



42

       inChannel.close();


43

       outChannel.close();


44

   }


45



46



47

   /**


48

    *通道之间的数据传输(直接缓冲区方式文件复制)


49

    */


50

   @Test


51

   public void test3()throws IOException{


52

       //使用直接缓冲区获取通道


53

       FileChannel inChannel = FileChannel.open(Paths.get("D://a.txt"),StandardOpenOption.READ);


54

       FileChannel outChannel = FileChannel.open(Paths.get("D://b.txt"),StandardOpenOption.WRITE, StandardOpenOption.READ);


55



56

       //从输入通道到输出通道


57

       inChannel.transferTo(0,inChannel.size(),outChannel);


58



59

       inChannel.close();


60

       outChannel.close();


61



62

   }


63



64



65



三、分散与聚集
 

 

1

/**


2

    *分散读取,聚集写入


3

    *@throws IOException


4

    */


5

   @Test


6

   public void test4() throws IOException {


7

       RandomAccessFile raf = new RandomAccessFile("D://a.txt", "rw");


8

       //获取通道


9

       FileChannel channel1 = raf.getChannel();


10

       //创建两个分散缓冲区


11

       ByteBuffer buff1 = ByteBuffer.allocate(10);


12

       ByteBuffer buff2 = ByteBuffer.allocate(1024);


13



14

       //将数据放入缓冲区(分散读取)


15

       ByteBuffer[] buffs = {buff1, buff2};


16

       channel1.read(buffs);


17



18

       for(ByteBuffer buf : buffs){


19

           buf.flip();//切换模式


20

      }


21



22

       System.out.println("缓冲区1:" + new String(buff1.array(),0,buff1.limit()));


23

       System.out.println("缓冲区2:" + new String(buff2.array(),0,buff2.limit()));


24

       //将数据从缓冲区中读出(聚集写入)


25

       RandomAccessFile raf2 = new RandomAccessFile("D://b.txt","rw");


26

       FileChannel channel2 = raf2.getChannel();


27

       channel2.write(buffs);


28



29

       channel1.close();


30

       channel2.close();


31

       raf.close();


32

       raf2.close();


33



34

   }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: