0
点赞
收藏
分享

微信扫一扫

Java NIO读取文件数据并输出

野见 2022-03-24 阅读 56


原理流程图

Java NIO读取文件数据并输出_保存数据

源码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();
}

NIO保存数据到文件



举报

相关推荐

0 条评论