文件读写filechannel

文件通道FileChannel是用于读取,写入,文件的通道。FileChannel只能被InputStream、OutputStream、RandomAccessFile创建。FileChannel通过字节流创建,那么操作的缓冲区肯定就是字节缓冲区。

public class FileChannelTest {
    
    // 使用通道读文件
    public void readData(File file) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            FileChannel fc = fis.getChannel();
            // 创建缓冲区
            ByteBuffer buf = ByteBuffer.allocate(1024);
            // 从通道读取数据到缓冲区
            fc.read(buf);
            // 反转缓冲区(limit设置为position,position设置为0,mark设置为-1)
            buf.flip();
            // 就是判断position和limit之间是否有元素
            while (buf.hasRemaining()) {
                // 按照字节的格式获取数据
                System.out.print((char) buf.get());
            }
            // 读完将缓冲区还原(position设置为0,limit设置为capacity,mark设置为-1)
            buf.clear();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    // 使用通道写文件
    public void writeData(File file , String data) {
        FileOutputStream fos = null;
        
        try {
            fos = new FileOutputStream(file);
            FileChannel fc = fos.getChannel();
            // 创建缓冲区
            ByteBuffer buf = ByteBuffer.allocate(1024);
            // 将数据装入缓冲区
            buf.put(data.getBytes());
            // 反转缓冲区(limit设置为position,position设置为0,mark设置为-1)
            buf.flip();
            // 将缓冲区中的数据写入到通道
            fc.write(buf);
            // 写完将缓冲区还原(position设置为0,limit设置为capacity,mark设置为-1)
            buf.clear();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args) {
        final FileChannelTest test = new FileChannelTest();
        File file = new File("E:\\StringTest.java");
        test.readData(file);
        test.writeData(file, "123456789");
    }
}

进行文件的 快速拷贝 

    FileInputStream in = new FileInputStream("sourcefile");
        FileOutputStream out = new FileOutputStream("destfile");
        FileChannel inchannel=in.getChannel();
        FileChannel outchannel = out.getChannel();
        outchannel.transferFrom(inchannel, 0,inchannel.size());



作者:sunpy
链接:https://www.jianshu.com/p/93c91fda2a9c
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

相关api参考https://docs.oracle.com/javase/8/docs/api/java/nio/channels/FileChannel.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值