Java File Read via ByteBuffer readFileNIO(String path, StringBuilder builder)

Here you can find the source of readFileNIO(String path, StringBuilder builder)

Description

read File NIO

License

Open Source License

Declaration

private static void readFileNIO(String path, StringBuilder builder) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

import java.io.RandomAccessFile;

import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
    private static void readFileNIO(String path, StringBuilder builder) throws IOException {
        RandomAccessFile aFile = null;
        FileChannel inChannel = null;
        try {//ww  w  .  j  av a  2  s . c  o  m
            aFile = new RandomAccessFile(path, "r");
            inChannel = aFile.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            while (inChannel.read(buffer) > 0) {
                buffer.flip();
                for (int i = 0; i < buffer.limit(); i++) {
                    builder.append((char) buffer.get());
                }
                buffer.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inChannel != null) {
                inChannel.close();
            }
            if (aFile != null) {
                aFile.close();
            }
        }
    }
}

Related

  1. readFileDataIntoBufferBE(FileChannel fc, final int size)
  2. readFileFragment(FileChannel fc, long pos, int size)
  3. readFileHeader(FileInputStream fpi)
  4. readFileIntoString(File localFile)
  5. readFileIntoString(String path)
  6. readFileToBuffer(java.io.File file)
  7. readFileToString(String path)
  8. readFileToString(String path)
  9. readFlashString(DataInputStream s)