import java.io.*; import java.nio.*; import java.io.FileInputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class BufferTransfer { public static void main(String[] args) throws Exception { FileInputStream finStream = new FileInputStream("testfile1.txt"); byte[] bArray = new byte[125]; FileChannel fchannel = finStream.getChannel(); ByteBuffer bytebuf = ByteBuffer.wrap(bArray); fchannel.read(bytebuf); bytebuf.flip(); bArray = new byte[bytebuf.capacity()]; System.out.print("Contents into buffer : "); while (bytebuf.hasRemaining()) { System.out.print((char) bytebuf.get()); } bytebuf.clear(); bytebuf.get(bArray, 0, bArray.length); System.out.print("\n"); System.out.print("Contents into Array : "); for (int i = 0; i < bytebuf.capacity(); i++) { System.out.print((char) bArray[i]); } } }