Java 基础 IO(字节流、字符流、大文件拷贝)

本文介绍了Java中字节流和字符流的读写,包括BufferedInputStream/BoundedOutputStream、FileReader/FileWriter以及按行读取的BufferedReader/BufferedWriter。此外,还探讨了大文件的高效拷贝方法,如使用FinChannel的多次取水和零拷贝技术(transferFrom/transferTo)。
摘要由CSDN通过智能技术生成

读写字节流

字节流缓冲

import java.io.*;

public class ByteReadWrite {
    public static void main(String[] args) {
        // 定义源文件
        File file = new File("C:\\Users\\we\\Downloads\\maven-practice.zip");
        // 获取文件名
        String fileName = file.getName();
        // 定义目标文件
        String targetPath = "E:\\" + fileName;
        // 使用自动关闭流的方式
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetPath))) {
            // 读取并写入字节流
            int len;
            byte[] buf = new byte[100 * 1024];
            while ((len = bis.read(buf)) != -1) {
                bos.write(buf, 0, len);
            }
            bos.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

读写字符流

字符流方式

import java.io.*;

public class StringReadWrite {
    public static void main(String[] args) {
        // 使用自动关闭流的方式
        try (FileReader reader = new FileReader("C:\\Users\\we\\Documents\\Todo.txt");
             FileWriter writer = new FileWriter("C:\\Users\\we\\Documents\\Copy.txt")) {
            // 读写文件
            char[] buf = new char[1024];
            while (reader.read(buf) != -1) {
                writer.write(new String(buf));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

字符流缓冲,按行读取

import java.io.*;

public class StringReadWriteByLine {
    public static void main(String[] args) {
        // 使用自动关闭流的方式
        try (FileReader fr = new FileReader("C:\\Users\\we\\Documents\\Todo.txt");
             FileWriter fw = new FileWriter("C:\\Users\\we\\Documents\\Copy.txt");
             BufferedReader reader = new BufferedReader(fr);
             BufferedWriter writer = new BufferedWriter(fw)) {
            // 读写文件
            String line;
            while ((line = reader.readLine()) != null) {
                writer.write(line);
                writer.newLine();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

大文件拷贝

FinChannel 直接读写

可以采用 NIO 的 FileChannel 多次重复"取水"的方式进行拷贝

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

public class BigFileCopy {
    public static void main(String[] args) {
        try (FileInputStream fis = new FileInputStream("E:\\cn_windows_10_enterprise_ltsc_2019_x64_dvd_9c09ff24.iso");
             FileOutputStream fos = new FileOutputStream("C:\\cn_windows_10_enterprise_ltsc_2019_x64_dvd_9c09ff24.iso");
             FileChannel inc = fis.getChannel();
             FileChannel outc = fos.getChannel()
        ) {
            ByteBuffer buffer = ByteBuffer.allocate(256 * 1024 * 1024);
            // 多次重复"取水"的方式
            while (inc.read(buffer) != -1) {
                buffer.flip();
                outc.write(buffer);
                buffer.clear();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

零拷贝方式

直接使用 FileChannel 的 transferFrom() 或 transferTo() 方法,底层实现采用的是零拷贝技术。

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

public class BigFileCopy2 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("E:\\cn_windows_10_enterprise_ltsc_2019_x64_dvd_9c09ff24.iso");
        FileOutputStream fos = new FileOutputStream("C:\\cn_windows_10_enterprise_ltsc_2019_x64_dvd_9c09ff24.iso");
        FileChannel srcChannel = fis.getChannel();
        FileChannel destChannel = fos.getChannel();
        destChannel.transferFrom(srcChannel, 0, srcChannel.size());
        destChannel.close();
        srcChannel.close();
        fis.close();
        fos.close();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值