当前位置:网站首页>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();
}
}
边栏推荐
- 进入小公司的初级程序员要如何自我提高?
- 【CTF】bjdctf_2020_babyrop
- GPIO初识
- Simple student management
- Kotlin Series 1: getting started with basics
- Difference between global shutter and roller shutter
- 36 krypton launched | cloud native database company "tuoshupai" completed a new round of strategic financing, and the valuation has reached the level of quasi Unicorn
- 全局快门和卷帘快门的区别
- Common English explanations in arm
- 使用base64,展示图片
猜你喜欢
随机推荐
swagger UI :%E2%80%8B
AI: the Elephant in Room
UEFI learning 3.6 - ACPI table on ARM QEMU
薄膜干涉数据处理
16.系统启动流程
文件的打开新建与存储
全局快门和卷帘快门的区别
Redis learning notes master-slave copy
Sequential representation and implementation of sequencelist -- linear structure
Jog运动模式
Opening, creating and storing files
ThinkPHP 2. X/3.0 vulnerability recurrence
【CTF】 2018_rop
General paging (1)
UEFI源码学习3.7 - NorFlashDxe
Servlet-02 生命周期
位绑定
[nanopi2 trial experience] the first step of bare metal
Redis learning notes - data type: hash
Bit binding
![[GYCTF2020]Blacklist](/img/a8/0f7231e5c498e6c89ff2e3bcb122f2.png)

![[极客大挑战 2019]HardSQL](/img/73/ebfb410296b8e950c9ac0cf00adc17.png)



