当前位置:网站首页>NiO example
NiO example
2022-06-23 09:40:00 【Linging_ twenty-four】
public class Main3 {
/** * nio Read the data in the file * @param args */
public static void main(String[] args) throws IOException {
readThenWrite();
}
/** * nio Read the data in the file * file -> channel -> buffer * @throws IOException */
public static void read() throws IOException{
// Get input stream
FileInputStream fis = new FileInputStream("D:\\ideaProject\\helloWorld\\src\\main\\resources\\read.txt");
// Get access to
FileChannel readChannel = fis.getChannel();
// Create a 1024 Byte size buffer
ByteBuffer buf = ByteBuffer.allocate(1024);
// Read data from channel to buffer , And returns the number of bytes read
int readBytes = readChannel.read(buf);
// Number of bytes read is not equal to -1, Explain that there are data
while(readBytes != -1){
byte[] bs = new byte[readBytes];
// Switch from write mode to read mode
buf.flip();
// The buffer still has data to read
while(buf.hasRemaining()){
buf.get(bs);
String str = new String(bs, 0, readBytes);
System.out.println(str);
}
// Clear buffer data
buf.clear();
// Read data from the channel to the buffer again , Know there's no data
readBytes = readChannel.read(buf);
}
fis.close();
}
/** * write file Existing problems , To be modified * buffer -> channel -> file * @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();
}
/** * File replication */
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){
// Switch to read mode
buffer.flip();
writeChannel.write(buffer);
buffer.clear();
}
fis.close();
fos.close();
}
}
边栏推荐
- 微信小程序:点击按钮频繁切换,重叠自定义markers,但是值不改变
- 表单重复提交问题
- What is BFC? What problems can BFC solve
- Redis learning notes - data type: string (string)
- A 32KB cache with direct mapping Memory exercises after class
- GPIO novice
- AI system frontier dynamics issue 38: Google has abandoned tensorflow?; Four GPU parallel strategies for training large models; Father of llvm: modular design determines AI future
- Redis learning notes - data type: hash
- Learn SCI thesis drawing skills (f)
- 启明星辰华典大数据量子安全创新实验室揭牌,发布两款黑科技产品
猜你喜欢

ionic5錶單輸入框和單選按鈕

Correspondence between three-tier architecture and SSM

学习SCI论文绘制技巧(F)

什么是BFC?BFC可以解决什么问题
[nanopi2 trial experience] the first step of bare metal
![[plugin:vite:import-analysis]Failed to resolve import “@/“ from ““.Does the file exist](/img/1f/dde52dc63de58d67f51161e318a9fb.png)
[plugin:vite:import-analysis]Failed to resolve import “@/“ from ““.Does the file exist

Redis learning notes - client communication protocol resp

AI system frontier dynamics issue 38: Google has abandoned tensorflow?; Four GPU parallel strategies for training large models; Father of llvm: modular design determines AI future

薄膜干涉数据处理

有没有人,计划开源一套工业级“秒杀”系统架构?
随机推荐
J. Med. Chem. | RELATION: 一种基于靶标结构的深度学习全新药物设计模型
Structure binary tree from preorder and inorder traversal for leetcode topic analysis
必须知道的RPC内核细节(值得收藏)!!!
UEFI source code learning 3.7 - norflashdxe
RBtree
The usage of lambda of C
ionic5表单输入框和单选按钮
Bit binding
Lua的基本使用
web--信息泄漏
Redis学习笔记—发布订阅
UEFI 源码学习4.1 - PciHostBridgeDxe
Chain representation and implementation of linklist ---- linear structure
Redis学习笔记—持久化机制之AOF
Redis学习笔记—事务
Learn SCI thesis drawing skills (E)
UEFI learning 3.6 - ACPI table on ARM QEMU
16. system startup process
Redis learning notes - data type: Set
ThinkPHP 2. X/3.0 vulnerability recurrence