zoukankan      html  css  js  c++  java
  • javaNio 通道和缓冲区

    /**
     * 大多数操作系统可以利用虚拟内存将文件或文件一部分映射到内存中,然后这个文件就可以被当做内存数组一样被访问;避免底层IO的开销<p>
     * 【通道】是一种用于磁盘文件的一种抽象;<br>
     * 它使我们可以访问诸如内存映射,文件加锁机制以及文件间快速数据传递等特性;
    *@date:2018年7月5日
    *@author:zhangfs
    
    
    */
    public class GetChannel {
    
        private static final int BSIZE=1024;
        public static void main(String[] args) throws IOException {
            
            //
            FileChannel fileChannel=new FileOutputStream("data.txt").getChannel();
            fileChannel.write( ByteBuffer.wrap("hello".getBytes()));//warp将已存在的数组包装到butebuffer中;
            fileChannel.close();
            //可读可写
            fileChannel=new RandomAccessFile("data.txt", "rw").getChannel();
            fileChannel.position(fileChannel.size());//移动到filechannel最后
            fileChannel.write(ByteBuffer.wrap(" world".getBytes()));
            fileChannel.close();
            //
            fileChannel=new FileInputStream("data.txt").getChannel();
            ByteBuffer buffer=ByteBuffer.allocate(BSIZE);
            fileChannel.read(buffer);
            buffer.flip();//让别人读取字符的准备;有利于获取最大速度
            while(buffer.hasRemaining()) {
                printnb((char)buffer.get());
            }
            
        }
    }
  • 相关阅读:
    centos编辑文件显示行号
    16.1
    [整理]正睿划水记
    [题解]UVA1519 Dictionary Size
    [题解]CF323C Two permutations
    [题解]CF1527D MEX Tree
    P2216 [HAOI2007]理想的正方形
    CF858D Polycarp's phone book
    ABC214F substrings
    每天一点小知识-20210810
  • 原文地址:https://www.cnblogs.com/zhangfengshi/p/9272308.html
Copyright © 2011-2022 走看看