Here you can find the source of readFileNIO(String path, StringBuilder builder)
private static void readFileNIO(String path, StringBuilder builder) throws IOException
//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(); } } } }