java读大文件

来源:互联网 发布:必利劲有依赖性吗 知乎 编辑:程序博客网 时间:2024/04/29 23:35

public static void main(String[] args) throws Exception {
   
int bufSize = 1024;
   
byte[] bs = new byte[bufSize];
    ByteBuffer byteBuf
= ByteBuffer.allocate(1024);
    FileChannel channel
= new RandomAccessFile("d://filename","r").getChannel();
   
while(channel.read(byteBuf) != -1) {
     
int size = byteBuf.position();
      byteBuf.rewind();
      byteBuf.get(bs);
     
// 把文件当字符串处理,直接打印做为一个例子。
      System.out.print(new String(bs, 0, size));
      byteBuf.clear();
    }
  }

原创粉丝点击