netty学习笔记09——filechannel的使用

netty学习笔记09——filechannel的使用

先写总结

  • java.io.FileInputStream#getChannel,从一个文件输入流获取channel,这个channel是个只读channel,在getChannel的时候临时构造的

    public FileChannel getChannel() {
        synchronized (this) {
            if (channel == null) {
                channel = FileChannelImpl.open(fd, path, true, false, this);
            }
            return channel;
        }
    }
    
  • java.io.FileOutputStream#getChannel,从一个输出流获取channel,这个channel是只能写的channel,和读的类似

    public FileChannel getChannel() {
        synchronized (this) {
            if (channel == null) {
                channel = FileChannelImpl.open(fd, path, false, true, append, this);
            }
            return channel;
        }
    }
    
  • channel.read(byteBuffer),读到buffer里

  • channel.write(byteBuffer),写到buffer里

写入内容

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannelWriter {
    public static void main(String[] args) throws IOException {
        //写一个字符串
        String str = "hello, sirius";
        //创建一个输出流
        FileOutputStream fileOutputStream = new FileOutputStream("./hello2.txt");

        //通过输出流获取文件channel
        //这个channel真实类型是FileChannelImpl
        FileChannel channel = fileOutputStream.getChannel();

        //创建一个缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        //将string,写入bytebuffer
        byteBuffer.put(str.getBytes());
        byteBuffer.flip();//切换读写模式,channel是从byteBuffer读取数据再进行写入的,所以要进行flip()
        channel.write(byteBuffer);
        fileOutputStream.close();

    }
}

读取内容

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannelRead {
    public static void main(String[] args) throws IOException {
        //创建一个输出流
        FileInputStream fileInputStream = new FileInputStream("./hello2.txt");

        //通过输出流获取文件channel
        //这个channel真实类型是FileChannelImpl
        FileChannel channel = fileInputStream.getChannel();
        //创建一个缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        channel.read(byteBuffer);//此时是写模式,从channel里读取数据写入到byteBuffer中
        byteBuffer.flip();//切换成为读模式
        System.out.println(new String(byteBuffer.array()));

    }
}

读写内容

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannelReadAndWriter {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("./hello2.txt");

        //通过输出流获取文件channel
        //这个channel真实类型是FileChannelImpl
        FileChannel readChannel = fileInputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream("./hello3.txt");
        FileChannel writeChannel = fileOutputStream.getChannel();
        //创建一个缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        while (true){
            byteBuffer.clear();//清空标记
            int count = readChannel.read(byteBuffer);//写模式,从channel读数据到byteBuffer
            if (count==-1){//读不到数据终止循环
                break;
            }
            byteBuffer.flip();//读模式
            writeChannel.write(byteBuffer);//从byteBuffer读数据到channel
        }
    }
}

快速复制

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class NIOFileChannelQuickCopy {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("./hello2.txt");

        //通过输出流获取文件channel
        //这个channel真实类型是FileChannelImpl
        FileChannel readChannel = fileInputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream("./hello3.txt");
        FileChannel writeChannel = fileOutputStream.getChannel();
        writeChannel.transferFrom(readChannel, 0, readChannel.size());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值