当前位置:网站首页>NIO例子
NIO例子
2022-06-23 09:32:00 【Linging_24】
public class Main3 {
/** * nio读取文件中的数据 * @param args */
public static void main(String[] args) throws IOException {
readThenWrite();
}
/** * nio读取文件中的数据 * 文件 -> channel -> buffer * @throws IOException */
public static void read() throws IOException{
// 获取输入流
FileInputStream fis = new FileInputStream("D:\\ideaProject\\helloWorld\\src\\main\\resources\\read.txt");
// 获取通道
FileChannel readChannel = fis.getChannel();
// 创建一个1024字节大小的缓冲区
ByteBuffer buf = ByteBuffer.allocate(1024);
// 从通道读取数据到缓冲区,并返回读取的字节数
int readBytes = readChannel.read(buf);
// 读取的字节数不等于-1,说明有数据
while(readBytes != -1){
byte[] bs = new byte[readBytes];
// 由写模式切换为读模式
buf.flip();
// 缓冲区还有数据可以读
while(buf.hasRemaining()){
buf.get(bs);
String str = new String(bs, 0, readBytes);
System.out.println(str);
}
// 清空缓冲区数据
buf.clear();
// 再次从通道中读取数据到缓冲区,知道没有数据
readBytes = readChannel.read(buf);
}
fis.close();
}
/** * 写入文件 存在问题,待修改 * buffer -> channel -> 文件 * @throws IOException */
public static void write() throws IOException {
FileOutputStream fos = new FileOutputStream("D:\\ideaProject\\helloWorld\\src\\main\\resources\\write.txt");
FileChannel channel = fos.getChannel();
ByteBuffer buf = ByteBuffer.allocate(1024);
buf.put("2312312312".getBytes(StandardCharsets.UTF_8));
System.out.println(buf.toString());
int writeBytes = channel.write(buf);
fos.close();
}
/** * 文件复制 */
public static void readThenWrite() throws IOException {
FileInputStream fis = new FileInputStream("D:\\ideaProject\\helloWorld\\src\\main\\resources\\read.txt");
FileOutputStream fos = new FileOutputStream("D:\\ideaProject\\helloWorld\\src\\main\\resources\\write.txt");
FileChannel readChannel = fis.getChannel();
FileChannel writeChannel = fos.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while(readChannel.read(buffer) != -1){
// 切换为读模式
buffer.flip();
writeChannel.write(buffer);
buffer.clear();
}
fis.close();
fos.close();
}
}
边栏推荐
猜你喜欢

薄膜干涉数据处理

swagger UI :%E2%80%8B

Zone d'entrée du formulaire ionic5 et boutons radio

UEFI source code learning 4.1 - pcihostbridgedxe

一元函数求极限三大方法---洛必达法则,泰勒公式

Map接口的注意事项
Redis learning notes - slow query analysis

Aiming at the overseas pet market, "grasshand" has developed an intelligent tracking product independent of mobile phones | early project

Correspondence between three-tier architecture and SSM
![[geek challenge 2019] hardsql](/img/73/ebfb410296b8e950c9ac0cf00adc17.png)
[geek challenge 2019] hardsql
随机推荐
An idea of using keep alive to cache data in vue3 form pages
How should junior programmers who enter a small company improve themselves?
Redis learning notes master-slave copy
ThinkPHP 2. X/3.0 vulnerability recurrence
使用base64,展示图片
微信小程序:点击按钮频繁切换,重叠自定义markers,但是值不改变
#gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
Cesium loading orthophoto scheme
【CTF】bjdctf_2020_babyrop
[GYCTF2020]Blacklist
Thin film interference data processing
UCOSII (learning notes)
UEFI 学习3.6 - ARM QEMU上的ACPI表
[GXYCTF2019]BabySQli
Redis学习笔记—Redis与Lua
线性表(LinkList)的链式表示和实现----线性结构
基于mediapipe的手势数字识别
The difference between ARM processor and 51 single chip microcomputer programming
ionic5表单输入框和单选按钮
[MRCTF2020]Ez_bypass