文件的读写

本文介绍了Java中两种文件读写方式,包括使用字节流进行读写,尤其适合处理图片和视频等,以及利用缓冲池提高读写效率,尤其在处理小文件时速度可提升约十倍。
摘要由CSDN通过智能技术生成

文件的读写

介绍两种文件的读写方式,一种是字节流的,一种使用缓冲池。

先说第一种,上代码

import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * 字节流
 */
@Slf4j
public class FileStream {

    public static void main(String[] args) {

        File file = new File("C:/Users/Administrator/Desktop/5f2bb5eb2c896.jpg");

        try {
            fileWriteStream(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 以字节流的形式读写文件,将内容复制到指定文件
     * @param file 原文件
     * @throws IOException
     */
    public static void fileWriteStream(File file) throws IOException {

        File fileNew = new File("C:/Users/Administrator/Desktop/5.jpg");
        if (fileNew.exists()){
            fileNew.delete();
        }

        long start = System.currentTimeMillis();

        if (file.exists()){

            FileInputStream fileInputStream = new FileInputStream(file);
            FileOutputStream fileOutputStream = new FileOutputStream(fileNew);

            byte[] bytes = new byte[2<<10];
            int read;
            while ((read = fileInputStream.read(bytes))!=-1){
                for (int i=0;i<read;i++){
                    fileOutputStream.write(bytes[i]);
                }
            }
            log.info("文件写入成功");

            fileOutputStream.close();
            fileInputStream.close();
        }else {
            log.info("文件不存在");
        }

        long end = System.currentTimeMillis();
        log.info("读写耗时:"+(end-start)+"毫秒");

    }
}

使用字节流是可以读写图片视频等文件的,但是使用字符流(这里没介绍)是不能读写图片视频的。

还有一种使用缓冲池的

import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * 用缓冲池来进行文件读写操作,会更高效
 */
@Slf4j
public class BufferStream {

    public static void main(String[] args) {

        File file = new File("C:/Users/Administrator/Desktop/5f2bb5eb2c896.jpg");

        try {
            bufferInOut(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用缓存池进行文件复制
     * @param file 原文件
     * @throws IOException
     */
    public static void bufferInOut(File file) throws IOException {

        File fileNew = new File("C:/Users/Administrator/Desktop/5.jpg");
        if (fileNew.exists()){
            boolean delete = fileNew.delete();
            log.info(delete?"存在同名文件,已删除":"存在同名文件,尝试删除失败");
        }

        //开始时间
        long start = System.currentTimeMillis();

        if (file.exists()) {
            FileInputStream fileInputStream = new FileInputStream(file);
            FileOutputStream fileOutputStream = new FileOutputStream(fileNew);

            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

            byte[] bytes = new byte[2<<8];
            int read;
            while ((read = bufferedInputStream.read(bytes))!=-1){
                for (int i=0;i<read;i++){
                    bufferedOutputStream.write(bytes[i]);
                }
            }
            log.info("文件复制完成");

            bufferedOutputStream.close();
            bufferedInputStream.close();
            fileOutputStream.close();
            fileInputStream.close();

        } else {
            log.info("文件不存在");
        }

        long end = System.currentTimeMillis();
        log.info("读写耗时:"+(end-start)+"毫秒");
    }
}

相较于前者,使用缓冲池读写效率更快。读写小文件可能没啥感觉,一般会快十倍左右。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值