简单实例
public static void main(String[] argv) throws Exception { // 获取Channel FileInputStream fin = new FileInputStream( "E:\study\HelloWorld\src\NIO\Main.java" ); FileChannel fc = fin.getChannel(); // 从Channel读数据到Buffer ByteBuffer byteBuffer = ByteBuffer.allocate(1024); fc.read(byteBuffer); // 将Buffer从读数据状态转换到输出数据状态 byteBuffer.flip(); // 从Buffer中读数据 while (byteBuffer.remaining()>0) { byte b = byteBuffer.get(); System.out.print(((char)b) ); } }