FileChannelExample { public MappedByteBuffer readFileToMemory(String sourceFile, int pos, int len) throws IOException { MappedByteBuffer mbb = null; FileInputStream fin = null; String infile = sourceFile; try { fin = new FileInputStream(infile); FileChannel fcin = fin.getChannel(); mbb = fcin.map(FileChannel.MapMode.READ_ONLY, pos, len); } catch (IOException ex) { throw ex; } finally { FileUtil.closeInput(fin); } return mbb; }  void readAndWrite(String sourceFile, String destFile); MappedByteBuffer readFileToMemory(String sourceFile, int pos, int len);  }
@Test public void readFileToMemory() throws IOException { String sourceFile = this.getClass().getResource("input/").getPath() + "testinput.txt"; MappedByteBuffer result = new FileChannelExample().readFileToMemory(sourceFile, 0, 4); Charset latin1 = Charset.forName("ISO-8859-1"); CharsetDecoder decoder = latin1.newDecoder(); CharBuffer cb = decoder.decode(result); System.out.println(cb.toString()); }