如何使用FileInputStream和FileOutputStream实现对文件的复制操作呢?

本文介绍了如何使用Java中的FileOutputStream和FileInputStream进行文件复制操作。详细阐述了FileOutputStream的构造方法及write、close方法,以及FileInputStream的read方法。通过实例展示了如何实现文件、图片和视频的复制功能。

一、FileOutputStream

FileOutputStream类,即文件输出流,是用于将数据写入 File的输出流。

它的构造方法主要有两个:

FileOutputStream的构造方法(1)

 使用以上这两个构造方法创建 FileOutputStream对象,写数据时会自动覆盖前面写过的数据。要避免这种情况,可以使用下面这种构造方法,在第二个参数填入true就能实现追加写入。

FileOutputStream的构造方法(2)

         我们使用到FileOutputStream类中的write()和close()方法,如下所示:

二、FileInputStream

FileInputStream类 从文件系统中的某个文件中获得输入字节。

它的构造方法和上面的类似,如图所示:

 我们主要使用到FileInputStream类中read()方法读取数据,如下所示:

  1. int read():读取一个字节并返回,没有字节返回-1.
  2. int read(byte[]): 读取一定量的字节数,并存储到字节数组中,返回读取到的字节数。

       三、文件复制、图片复制、视频复制——代码实现

        接下来我们将使用FileInputStream和FileOutputStream来实现文件的复制操作。

import java.io.*;

public class FileInputStreamCopy {
    public static void main(String[] args) {
        // 文件对象
        File wordFile = new File("D:\\programming\\IDEA\\save\\docx\\03 虚拟仪器报告-实验三.doc");
        File pictureFile = new File("D:\\programming\\IDEA\\save\\docx\\QQ图片20210901172834.jpg");
        File videoFile = new File("D:\\programming\\IDEA\\save\\docx\\VID_20210502_143040.mp4");

        // 文件复制
        wordCopy(wordFile);

        // 图片复制
        pictureCopy(pictureFile);

        // 视频复制
        videoCopy(videoFile);

    }

    public static void wordCopy(File file){
        FileInputStream fis = null;
        FileOutputStream fos = null;

        // 准备好复制过来的文件的新名字
        String[] names = file.getPath().split("\\.");
        String name = names[0]+"Copy."+names[1]; // 重命名
        try {
            // 文件字节输出、输出流对象
            fis = new FileInputStream(file);
            fos = new FileOutputStream(name);
            // 读数据
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len=fis.read(bytes))!=-1){
                // 写数据
                fos.write(bytes,0,len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                // 释放资源
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }



    }

    public static void pictureCopy(File file){
        FileInputStream fis = null;
        FileOutputStream fos = null;

        // 为复制过来的文件准备新名字
        String[] names = file.getPath().split("\\.");
        String name = names[0]+"Copy."+names[1];    // 新名字
        try {
            // 文件字节输入输出流
            fis = new FileInputStream(file);
            fos = new FileOutputStream(name);

            // 读数据
            int len=0;
            byte[] bytes = new byte[1024];
            while ((len=fis.read(bytes))!=-1){
                fos.write(bytes,0, len);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            // 释放资源
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void videoCopy(File file){
        FileInputStream fis = null;
        FileOutputStream fos = null;

        // 为复制过来的文件重命名
        String[] names = file.getPath().split("\\.");
        String name = names[0]+"Copy."+names[1];

        try {
            // 文件字节输入输出对象
            fis = new FileInputStream(file);
            fos = new FileOutputStream(name);

            // 读数据
            int len=0;
            byte[] bytes = new byte[1024];
            while ((len=fis.read(bytes))!=-1){
                // 写数据
                fos.write(bytes,0,len);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                // 释放资源
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

没有运行程序前:

 运行程序效果图:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值