java nio 之FileChannel

来源:互联网 发布:魔术蜘蛛软件2.0 编辑:程序博客网 时间:2024/05/24 01:17
package com.nio;import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;public class FileChannelTest{      public static void main(String[] args) throws FileNotFoundException   {      RandomAccessFile randomAccessFile=new RandomAccessFile("/home/plcm/test.txt", "rw");      FileChannel fileChannel=randomAccessFile.getChannel();      ByteBuffer buffer=ByteBuffer.allocate(1024);      try      {         int byteReaded=fileChannel.read(buffer);         while (byteReaded!=-1)         {            System.out.println("Read " + byteReaded);            buffer.flip();            while (buffer.hasRemaining())            {               System.out.println((char)buffer.get());            }            buffer.clear();            byteReaded=fileChannel.read(buffer);                     }         fileChannel.close();      }      catch (IOException e)      {         // TODO implement catch IOException         throw new UnsupportedOperationException("Unexpected Exception", e);               }   }}


Read 25aaaaaabbbbbcccccddddd



aaaaaabbbbbcccccddddd

0 0
原创粉丝点击