NIO之channel和buffer的运用

例1:将hello,world!输出到文件a.txt中

在这里插入图片描述

public class NIOFilechannel {
    public static void main(String[] args) throws IOException {
        // 创建一个输出流
        FileOutputStream fos = new FileOutputStream("C:\\Users\\whatsoooever\\Desktop\\a.txt");
        // 获取输出流的channel
        FileChannel fc = fos.getChannel();
        // 创建一个缓冲区,并分配1024个字节
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        // 将字符串转化为字节写入到缓冲区中
        byteBuffer.put("hello,world!".getBytes());
        // 由于要从缓冲区中读数据写入到channel中,所以先反转
        byteBuffer.flip();
        fc.write(byteBuffer);
        
        fc.close();
        fos.close();
    }
}
例2:将文件a.txt中的内容打印到控制台上

在这里插入图片描述

public class NIOFilechannel {
    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\whatsoooever\\Desktop\\a.txt");
        FileInputStream fis = new FileInputStream(file);
        FileChannel fc = fis.getChannel();
        ByteBuffer bf = ByteBuffer.allocate((int)file.length());
        fc.read(bf);
        System.out.println(new String(bf.array()));
        fc.close();
        fis.close();
    }
}
例3:将文件a.txt中的内容复制到b.txt中

在这里插入图片描述

public class NIOFilechannel {
    public static void main(String[] args) throws IOException {

        FileInputStream fis = new FileInputStream("C:\\Users\\whatsoooever\\Desktop\\a.txt");
        FileChannel fc1 = fis.getChannel();

        FileOutputStream fos = new FileOutputStream("C:\\Users\\whatsoooever\\Desktop\\b.txt");
        FileChannel fc2 = fos.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        while (true) {
            // 重要操作,一定要clear
            byteBuffer.clear();
            int read = fc1.read(byteBuffer);
            if (read == -1) {
                break;
            }
            // 读写切换
            byteBuffer.flip();
            fc2.write(byteBuffer);
        }

        fc1.close();
        fc2.close();
        fis.close();
        fos.close();
    }
}
例4:通过tansferfrom的方式将文件a.txt中的内容复制到c.txt中,无需bytebuffer
public class NIOFilechannel {
    public static void main(String[] args) throws IOException {

        File file = new File("C:\\Users\\whatsoooever\\Desktop\\a.txt");
        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream("C:\\Users\\whatsoooever\\Desktop\\c.txt");

        FileChannel resource = fis.getChannel();
        FileChannel des = fos.getChannel();

        des.transferFrom(resource,0,file.length());

        des.close();
        resource.close();
        fis.close();
        fos.close();
    }
}
例5:通过mappedByteBuffer将文件a.txt直接在内存(堆外内存)修改,操作系统不需要拷贝一次
public class MappedByteBufferTest {
    public static void main(String[] args) throws IOException {
        RandomAccessFile acf = new RandomAccessFile("C:\\Users\\whatsoooever\\Desktop\\a.txt","rw");
        FileChannel fc = acf.getChannel();
        // 参数1:使用读写模式
        // 参数2:可以直接修改的起始位置
        // 参数3:将文件映射到内存的大小
        MappedByteBuffer mb = fc.map(FileChannel.MapMode.READ_WRITE,0,5);
        mb.put(0,(byte)'H');
        mb.put(3,(byte)'Z');

        fc.close();
        acf.close();
    }
}

原文件中内容:hello,world!
新文件中内容:HelZo,world!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值