NIO - 文件的简单操作

1. 将数据写进文件

public class NioFileChannelDemo1 {
    public static void main(String[] args) throws Exception {
        String str = "hello world";

        // 创建一个输出流 -> channel
        FileOutputStream fileOutputStream = new FileOutputStream("路径/文件名.后缀");
        FileChannel fileChannel = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024); // 创建一个缓冲区
        byteBuffer.put(str.getBytes(StandardCharsets.UTF_8)); // 将str放入buffer
        byteBuffer.flip(); // 从读状态切换到写状态
        fileChannel.write(byteBuffer); // 将buffer数据写入到channel
        fileOutputStream.close();
    }
}

2. 从文件中读取数据

public class NioFileChannelDemo2 {
    public static void main(String[] args) throws Exception {
        // 创建文件的输入流
        File file = new File("路径/文件名.后缀");
        FileInputStream inputStream = new FileInputStream(file);

        FileChannel fileChannel = inputStream.getChannel(); // 通过输入流获取 chennel
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length()); // 创建缓冲区
        fileChannel.read(byteBuffer); // 将通道的数据读入到buffer
        System.out.println(new String(byteBuffer.array()));
        inputStream.close();
    }
}

3. 拷贝文件

3.1 方式一

public class NioFileChannelDemo3 {
    public static void main(String[] args) throws Exception {
        FileInputStream inputStream = new FileInputStream("路径/源文件名.后缀");
        FileChannel fileChannel1 = inputStream.getChannel();

        FileOutputStream outputStream = new FileOutputStream("路径/拷贝后文件名.后缀"); // 不存在则创建
        FileChannel fileChannel2 = outputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(5);
        while (true) {
            byteBuffer.clear(); // 清空buffer
            int read = fileChannel1.read(byteBuffer); // 返回值表示读取的数据是多少
            if (read == -1) { // 表示读完
                break;
            }
            byteBuffer.flip();
            fileChannel2.write(byteBuffer);
        }

        inputStream.close();
        outputStream.close();
    }
}

3.2 方式二

public class NioFileChannelDemo4 {
    public static void main(String[] args) throws Exception {
        FileInputStream inputStream = new FileInputStream("路径/源文件名.后缀");
        FileOutputStream outputStream = new FileOutputStream("路径/拷贝后文件名.后缀"); // 不存在则创建

        FileChannel sourceCh = inputStream.getChannel();
        FileChannel destch = outputStream.getChannel();
        destch.transferFrom(sourceCh, 0, sourceCh.size()); // 拷贝

        sourceCh.close();
        destch.close();
        inputStream.close();
        outputStream.close();
    }
}

3.3 方式三

public class NioFileChannelDemo5 {
    public static void main(String[] args) throws IOException {
        File source = new File("路径/源文件名.后缀");
        File dest = new File("路径/拷贝后文件名.后缀");
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {
            inChannel = new FileInputStream(source).getChannel();
            outChannel = new FileOutputStream(dest).getChannel();
            MappedByteBuffer buf = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
            outChannel.write(buf);

            inChannel.close();
            outChannel.close();
        }catch(Exception e){
            e.printStackTrace();
        } finally {
            inChannel.close();
            outChannel.close();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LF3_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值