使用NIO——FileChannel按行读取文件

读取字符文件时,如果中途我们想输出字符串要特别小心。下面的代码在输出文本内容时,很可能产生中文读取再输出的乱码:

private static void byteBuffer() throws IOException {
  RandomAccessFile randomAccessFile = new RandomAccessFile("/Users/zhengwei/lanqiao/ConditionOperator.java", "rw");
  FileChannel channel = randomAccessFile.getChannel();
  ByteBuffer buffer = ByteBuffer.allocate(10);
  int bytesRead = channel.read(buffer);
  while (bytesRead != -1) {
    // System.out.println("读取字节数:"+bytesRead);
    //之前是写buffer,现在要读buffer
    buffer.flip();// 切换模式,写->读
    System.out.print(Charset.forName("utf-8").decode(buffer));  // 这样读取,如果10字节恰好分割了一个字符将出现乱码
    buffer.clear();// 清空,position位置为0,limit=capacity
    //  继续往buffer中写
    bytesRead = channel.read(buffer);
  }
  randomAccessFile.close();
}

如何设计一个按行读取的NIO?

具体思路是:设置两个缓冲区,一大一小,大的缓冲区为每次读取的量,小的缓冲区存放每行的数据(确保大小可存放文本中最长的那行)。读取的时候判断是不是换行符13,是的话则返回一行数据,不是的话继续读取,直到读完文件。

private static void readLine() throws IOException {
  RandomAccessFile randomAccessFile = new RandomAccessFile("/Users/zhengwei/lanqiao/ConditionOperator.java", "rw");
  FileChannel channel = randomAccessFile.getChannel();
  ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);
  int bytesRead = channel.read(buffer);
  ByteBuffer stringBuffer = ByteBuffer.allocate(20);
  while (bytesRead != -1) {
    System.out.println("读取字节数:" + bytesRead);
    //之前是写buffer,现在要读buffer
    buffer.flip();// 切换模式,写->读
    while (buffer.hasRemaining()) {
      byte b = buffer.get();
      if (b == 10 || b == 13) { // 换行或回车
        stringBuffer.flip();
        // 这里就是一个行
        final String line = Charset.forName("utf-8").decode(stringBuffer).toString();
        System.out.println(line + "----------");// 解码已经读到的一行所对应的字节
        stringBuffer.clear();
      } else {
        if (stringBuffer.hasRemaining())
          stringBuffer.put(b);
        else { // 空间不够扩容
          stringBuffer = reAllocate(stringBuffer);
          stringBuffer.put(b);
        }
      }
    }
    buffer.clear();// 清空,position位置为0,limit=capacity
    //  继续往buffer中写
    bytesRead = channel.read(buffer);
  }
  randomAccessFile.close();
}

private static ByteBuffer reAllocate(ByteBuffer stringBuffer) {
  final int capacity = stringBuffer.capacity();
  byte[] newBuffer = new byte[capacity * 2];
  System.arraycopy(stringBuffer.array(), 0, newBuffer, 0, capacity);
  return (ByteBuffer) ByteBuffer.wrap(newBuffer).position(capacity);
}

 

你可能感兴趣的:(java)