Java基础(二)

1.文件拷贝方式

1.1 传统字节流拷贝(FileInputStream + FileOutPutStream)
    public static void main(String[] args) throws IOException {
        try (InputStream is = new FileInputStream("C:\\source.txt");
             OutputStream os = new FileOutputStream("D:\\target.txt")) {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

特点:基础方法,直接按字节或缓冲区读写

效率:最低(适合小文件)

1.2 缓冲流优化拷贝(BufferedInputStream + BufferedOutPutStream)
    public static void main(String[] args) throws IOException {
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\source.txt"));
             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\target.txt"))) {
            byte[] buffer = new byte[8192]; // 缓冲区越大,性能越好(通常 8KB~64KB)
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

特点:通过缓存区减少了I/O次数

效率:比传统字节流提升2~5倍

1.3 NIO的Files.copy方法
    public static void main(String[] args) throws IOException {
        Path source = Paths.get("c:\\source.txt");
        Path target = Paths.get("d:\\target.txt");
        Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
    }

特点:单行代码就完成了拷贝,底层自动优化

效率:接近最高效率(适合大多数场景)

1.4 NIO的FileChannel通道拷贝
    public static void main(String[] args) throws IOException {
        try (FileChannel sourceChannel = new FileInputStream("c:\\source.txt").getChannel();
             FileChannel targetChannel = new FileOutputStream("d:\\target.txt").getChannel()) {
            sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
        }
    }

特点:利用通道(Channel)直接传输数据

效率:大文件性能最佳(利用零拷贝技术) 

1.5 内存映射文件拷贝(MappedByteBuffer)
    public static void main(String[] args) throws IOException {
        try (RandomAccessFile sourceFile = new RandomAccessFile("c:\\source.txt", "r");
             RandomAccessFile targetFile = new RandomAccessFile("d:\\target.txt", "rw")) {
            FileChannel sourceChannel = sourceFile.getChannel();
            MappedByteBuffer buffer = sourceChannel.map(FileChannel.MapMode.READ_ONLY, 0, sourceChannel.size());
            targetFile.getChannel().write(buffer);
        }
    }

特点:将文件映射到内存直接操作

效率:适合超大文件(但实现复杂,需谨慎处理内存)

总结:优化选择使用Files.copy简洁高效,如果是大文件推荐使用FileChannel

2.中止线程的方式

2.1 使用stop()终止线程

不推荐,强行中止线程,是不安全的,可能会导致资源泄露

2.2 使用interrupt()中止线程

推荐使用,不会直接强行中止,而是打上标记,通知线程需要中止,是否中止由线程决定。

配合使用isInterrupted判定是否被中断,以及interrupted()判断是否被中断,并清除当前中断的状态

注意:多个线程使用的同一个变量使用volatile来修饰,那每个线程都能读取最新的值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值