zoukankan      html  css  js  c++  java
  • NIO——FileChannel

     1 package javaj.nio.channel;
     2 
     3 import java.io.RandomAccessFile;
     4 import java.nio.ByteBuffer;
     5 import java.util.Objects;
     6 
     7 /**
     8  * 文件通道
     9  *
    10  * @author wangYuBai
    11  * @create 2018-11-23-18:54
    12  */
    13 public class FileChannel {
    14     public static void main(String[] args) {
    15         try {
    16             RandomAccessFile randomAccessFile = new RandomAccessFile("疑问.txt", "rw");
    17             java.nio.channels.FileChannel fileChannel = randomAccessFile.getChannel();
    18             if (Objects.isNull(randomAccessFile)) {
    19                 System.out.println("yes");
    20             }
    21             /**
    22              * 初始化一个 Buffer 后
    23              * position = 0
    24              * limit = capacity = 分配给Buffer的数量
    25              */
    26             ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
    27             System.out.println("limit:" + byteBuffer.limit() + '
    ' + "position:" + byteBuffer.position() +
    28                     "
    capacity:" + byteBuffer.capacity());
    29             /**
    30              * 在使用过read()方法后,position 会被更新为实际读取文件的字节数,position = byteNumber
    31              * read()会从当前文件位置开始read
    32              */
    33             int byteNumber = fileChannel.read(byteBuffer);
    34             System.out.println("limit:" + byteBuffer.limit() + '
    ' + "position:" + byteBuffer.position() +
    35                     "
    capacity:" + byteBuffer.capacity());
    36             while (byteNumber != -1) {
    37                 System.out.println("READ" + byteNumber);
    38                 System.out.println("limit:" + byteBuffer.limit() + '
    ' + "position:" + byteBuffer.position());
    39                 /**
    40                  *     flip()
    41                  *     public final Buffer flip() {
    42                  *         limit = position;
    43                  *         position = 0;
    44                  *         mark = -1;
    45                  *         return this;
    46                  *     }
    47                  */
    48                 byteBuffer.flip();
    49                 System.out.println("limit:" + byteBuffer.limit() + '
    ' + "position:" + byteBuffer.position());
    50                 /**
    51                  * hasRemaining()是根据 position < limit 来进行判断的
    52                  */
    53                 while (byteBuffer.hasRemaining()) {
    54                     /**
    55                      * getChar()读取两个字节,然后合并到一个char中
    56                      * 最后将 position + 2
    57                      */
    58                     char c = byteBuffer.getChar();
    59                     System.out.println("limit:" + byteBuffer.limit() + '
    ' + "position:" + byteBuffer.position());
    60                 }
    61                 /**
    62                  *      clear()
    63                  *      public final Buffer clear() {
    64                  *         position = 0;
    65                  *         limit = capacity;
    66                  *         mark = -1;
    67                  *         return this;
    68                  *     }
    69                  */
    70                 byteBuffer.clear();
    71                 /**
    72                  * position = limit
    73                  * channel 已经被读取到了末尾,返回 -1
    74                  */
    75                 byteNumber = fileChannel.read(byteBuffer);
    76             }
    77             randomAccessFile.close();
    78         } catch (Exception e) {
    79             e.printStackTrace();
    80         }
    81     }
    82 }
    View Code

    从JDK4开始,Java IO,是通过字节流或者是字符流来进行IO。

      IO是阻塞的,也就是说一个read()方法,读取文件时,如果此时没有文件,则方法会一直在这里等

    到了JDK7,出现 NIO。

      NIO是非阻塞的,当使用一个read()方法读取文件时,如果文件没有准备就绪,则会返回一个Code,告诉调用者没准备好

    这里阻塞和非阻塞和同步异步没有任何联系。

  • 相关阅读:
    设计模式
    设计模式
    设计模式
    设计模式
    【Sublime】许可证 及 相关设置
    【Linux】跳过ubuntu grub2引导,使用Windows引导ubuntu
    【Linux】Windows 7下硬盘安装Ubuntu 14.10图文教程
    【ACM】连连看 hdu1175
    【算法】约瑟夫环 C++源代码
    【Java】配置JAVA的环境变量
  • 原文地址:https://www.cnblogs.com/AI-Cobe/p/10009813.html
Copyright © 2011-2022 走看看