原理流程图
源码demo
public static void main(String[] args) throws Exception {
//拿到文件
File file = new File("d:\\test\\write.txt");
FileInputStream inputStream = new FileInputStream(file);
//创建管道,把文件放入通道
FileChannel fileChannel = inputStream.getChannel();
//创建Buffer
ByteBuffer buffer = ByteBuffer.allocate((int)file.length());
//从通道读数据放入缓冲区
fileChannel.read(buffer);
System.out.println(new String(buffer.array()));
//关闭与清除
fileChannel.close();
inputStream.close();
buffer.clear();
}