非阻塞IO初探

RandomAccessFile 随机读取文件内容

  • 构造方法 RandomAccessiFile raf=new RandomAccessFile("filename",mode);

  • 读取任意位置的文件内容

  • 两个方法
    获得文件指针当前指向的位置: raf.getfilepoint();

    设置文件指针的位置: raf.seek(int position);
    
  • RandomAccessFile和输入输出流类似使用Read()和Write()方法

  • 在指定位置进行内容的插入内容,需要将指针位置以后内容放在另外的文件进行保存或者 写入到Buffer中进行保存,否则插入的内容会覆盖之前的

Buffer

  • 多种进本数据类型的实现类,主要的实现类ByteBuffer,CharBuffer

  • 主要方法put(),get()

  • 构造方法
    通过allocate(int capacity)方法获得
    ByteBuffer bytebuffer=ByteBuffer.allocate(10);初始化容量为10

  • 主要参数
    position,limit,capacity

  • 读取和输出

    输入
    positon初始值是0,limit进行数据的封装
    添加数据完毕以后调用byteBufer.flip()方法,为输出做准备

    输出
    byteBuffer.clear()调用进行数据读入做准备

  • 简单例子
    public class BufferTest {
    @Test
    public void initTest(){
    CharBuffer buffer=CharBuffer.allocate(8);
    System.out.println("capacity="+buffer.capacity());
    System.out.println("position="+buffer.position());
    System.out.println("limit="+buffer.limit());

      System.out.println("\n 添加元素以后.....");
      buffer.put('s');
      buffer.put('h');
      buffer.put('f');
      System.out.println("position="+buffer.position());
      System.out.println("capacity="+buffer.capacity());
      System.out.println("limit="+buffer.limit());
              System.out.println("\n 添加元素结束以后准备取出元素....");
      buffer.flip();
      System.out.println("position="+buffer.position());
      System.out.println("limit="+buffer.limit());
      System.out.println("capacity="+buffer.capacity());  
      System.out.println("\n 取出元素以后......");
      System.out.println("取出第一个元素buffer[0]="+buffer.get());
      System.out.println("position="+buffer.position());
      System.out.println("capacity="+buffer.capacity());
      System.out.println("limit="+buffer.limit());
    
      System.out.println("\n 取出元素以后为添加元素做准备....");
      buffer.clear();
      System.out.println("position="+buffer.position());
      System.out.println("limit="+buffer.limit());
      System.out.println("capacity="+buffer.capacity());
              }
    

Channel

  • 主要的实现类FileChannel等

  • 类似于输入输出流

  • 指定文件中的内容映射到buffer中
    FileChannel进行映射
    ** 映射成ByteBuffer **
    MapedByBuffer bytebuffer=FileChannel.map()

  • 不能直接对Channel进行读取和写入, 必须Buffer

  • 构造方法,通过文件流进行获得
    FileChannel in=Fileinputstream(new File()).getChannel();
    FileChannel out=FileoutputStream(new File()).getChannel();

  • 读取
    MapedBybyteBuffer bytebuffer=in.map(FileChannel.mapmodel.read_only,0,file.length);

  • 文件复制
    public class ChannelTest {
    @Test
    public void changeTest() throws IOException{
    /*
    * 获取类似文件流的通道channel
    */

      File file=new File("E:/javaFile/shf.txt");
      FileChannel inChannel=new FileInputStream(file).getChannel();
      FileChannel outChannel=new FileOutputStream(new File("E:/javaFile/haifeng.txt")).getChannel();
      /*
       * 将Channel数据映射到byteBuffer中
       */
      
      MappedByteBuffer buffer=inChannel.map(FileChannel.MapMode.READ_ONLY, 0,file.length());
      
      /*
       * 将buffer中的数据直接输出到文件中
       */
      outChannel.write(buffer);
      /*
       * 不能讲buffer内容直接输出
       * 需要进行解码器进行解码
       * 转换成charBuffer
       * 进行输出
       */
      //System.out.println(buffer);
      
      /*
       * 必须将buffer进行清空
       * 为取出内容做准备
       * buffer.clear()
       */
      buffer.clear();
      Charset charset=Charset.forName("utf-8");
    
      CharsetDecoder decoder=charset.newDecoder();
      CharBuffer charBuffer=decoder.decode(buffer);
      System.out.println(charBuffer);
      
    }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转自 http://www.ibm.com/developerworks/cn/education/java/j-...
    抓兔子的猫阅读 2,355评论 0 22
  • 前言: 之前的文章《Java文件IO常用归纳》主要写了Java 标准IO要注意的细节和技巧,由于网上各种学习途径,...
    androidjp阅读 2,934评论 0 22
  • 传统IO和普通IO的区别 传统IO:面向流,阻塞IO(Blocking), selector NIO:面向缓冲区,...
    he_321阅读 627评论 0 4
  • 已经是晚上10点,林小欣匆匆忙忙整理好桌上的东西,准备下班。最近天天加班,事情多,还得忍受脾气暴躁的老板,每天上班...
    知言春秋阅读 352评论 0 1
  • 喜欢你 我喜欢草莓,但草莓汁不行,草莓糖也不行。 就像我喜欢你时,长得像你不行,脾气像你也不行,因为我喜欢的是你。...
    我最爱胡歌阅读 1,546评论 0 0