zoukankan      html  css  js  c++  java
  • 文件通道

    简单的使用nio读取文件的例子

     1 public static void main(String[] args) throws Exception {
     2         FileInputStream fis = new FileInputStream("/home/tp/1.txt");
     3         FileChannel fc = fis.getChannel();//获取通道
     4         ByteBuffer bb = ByteBuffer.allocate(1024);//创建一个缓冲区
     5         fc.read(bb);把数据读到缓冲区
     6         bb.flip();//重设buffer,limit设置为position,position设置为0
     7         while(bb.hasRemaining()){//查看position和limit之间是否还有元素
     8             byte b = bb.get();//读取当前位置的整数
     9             System.out.print((char)b);
    10         }
    11         fis.close();
    12     }

    简单使用nio写文件的例子

     1 public static void main(String[] args) throws Exception {
     2         FileOutputStream fileOutputStream = new FileOutputStream("/home/tp/2.txt");//如果没有这个文件会自己创建
     3         byte message[] = { 83, 111, 109, 101, 32, 98, 121, 116, 101,115, 46 };
     4         FileChannel fileChannel = fileOutputStream.getChannel();//获取通道
     5         ByteBuffer buffer = ByteBuffer.allocate(1024);//创建一个缓冲区
     6         for (int i = 0; i < message.length; ++i) {
     7             buffer.put(message[i]);//写入到缓冲区中。
     8         }
     9         buffer.flip();//重设buffer,limit设置为position,position设置为0
    10         fileChannel.write(buffer);//把缓存写到通道里。
    11         fileOutputStream.close();
    12     }

    结果:

    在文件中打印 some bytes.

    FileChannel无法设置为非阻塞模式的,它总是运行在阻塞模式下的。

    对于某个特定位置进行数据的读写操作,

    可以先调用position方法获取fileChannel的当前位置

    也可以通过position(long pos)方法设置FileChannel的当前位置

    size()方法将返回该实例所关联文件的大小

    force(boolean metaData)方法将通道里尚未写入磁盘的数据强制写到磁盘上,metaData为true将包含权限。

    truncate(n)截取文件前40字节,对文件本身发生作用,需要有写权限。

    FileChannel有两个独有的方法:transferFrom()和transferTo(),因此,Channel-to-Channel传输中通道之一必须是fileChannel。直接的传输通道不会更新与某个FileChannel关联的position值。直接的通道传输不会更新与某个FileChannel关联的

    position值。对于传输数据来源是一个文件的transferTo方法,如果position+count大于文件的size值,传输会在文件末尾终止。

     1 public static void main(String[] args) throws Exception {
     2         RandomAccessFile fromFile = new RandomAccessFile("/home/tp/1.txt", "rw");
     3         FileChannel fromChannel = fromFile.getChannel();
     4         RandomAccessFile toFile = new RandomAccessFile("/home/tp/2.txt", "rw");
     5         FileChannel toChannel = toFile.getChannel();
     6         long position =0;
     7         long count = fromChannel.size();
     8         //toChannel.transferFrom(fromChannel, position, count);//可以与下面那行替换。
     9         fromChannel.transferTo(position, count, toChannel);//调用tranferTo方法把自己里面从position开始count字节的数据发到toChannel去。
    10         toChannel.close();  
    11         toFile.close();  
    12         fromChannel.close();  
    13         fromFile.close();  
    14     }

    FileChannelImpl的源码:  http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/sun/nio/ch/FileChannelImpl.java#FileChannelImpl

  • 相关阅读:
    《Windows游戏编程技巧大师》就DirectDraw而创建DirectDraw知识笔记
    中国省城市列表(中国的性格+拼音)
    Android NDK的C++11标准支持
    DFGUI-- 标签交换 Tabstrip
    Ubuntu更改hosts档
    如何完成Nexus 9上电后激活过程
    Exception dispatching input event. use XlistView
    音乐TV2015校园招聘A第二大发行量(对中国科学院大学站)
    github basic usage in windows
    HDU 1501 Zipper(DP,DFS)
  • 原文地址:https://www.cnblogs.com/tp123/p/6433701.html
Copyright © 2011-2022 走看看