原理流程图

源码demo
public static void main(String[] args) throws Exception {
String str="你好,chen";
//创建Buffer
ByteBuffer buffer = ByteBuffer.allocate(1024);
//将str放入Buffer
buffer.put(str.getBytes());
//反转读写模式
buffer.flip();
//创建文件
FileOutputStream outputStream = new FileOutputStream("d:\\test\\write.txt");
//创建Channel,把文件放入通道
FileChannel fileChannel = outputStream.getChannel();
//把Buffer内容放入Channel
fileChannel.write(buffer);
fileChannel.close();
outputStream.close();
//清除缓冲区
buffer.clear();
System.out.println("结束");
}
debug代码,看到OutputStream内置了Channel

NIO读取文件数据并输出