文件拷贝

本文详细介绍了一种使用Java字节流进行文件拷贝的方法,包括检查源文件存在性、创建目标文件目录及实际拷贝过程。通过具体代码实现,展示了如何高效地从源文件读取数据并写入目标文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 目的:
    把源文件中的内容拷贝一份至目标文件。
    在这里插入图片描述
  2. 思想:
  • 定义一个类完成所有的前提条件,和文件拷贝的具体内容;
  • 因为要把源文件的内容拷贝到目标文件,操作的是文件的内容,所以需要使用输入输出流,字节流使用范围比较广泛,此处使用字节流;
    字节输入流InputStream,字节输出流OutputStream;
  • 前提1:先从源文件中读取内容,此时要保证的是源文件必须存在,只有源文件存在才能从中读取内容;
  • 前提2:把读取到内容输出到目标文件,此时要确保所给文件的父路径都存在,如果不存在,则创建所有的目录;
  • 完成具体拷贝。一边从源文件读取数据,一边输出到目标文件。
    (1)创建文件的输入输出流对象;
    (2)从源文件中读取数据(read),直到源文件为空;
    (3)将读到的数据输出(writer)到目标文件。
  1. 具体代码:
package www.filecopy;

import java.io.*;

class FileCopyUtil {
    //构造方法私有化
    private FileCopyUtil() {
    }

    //前提1:保证源文件存在
    public static boolean sourceFileIsExists(String sourcePath) {
        File sourceFile = new File(sourcePath);
        return sourceFile.exists();
    }

    //前提2:判断目标文件的父路径是否存在,不存在的话创建目录
    public static void createParentDir(String destPath) {
        File destFile = new File(destPath);
        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs();
        }
    }
//3. 拷贝的具体实现
    public static boolean fileCopy(String sourcePath, String descPath) throws IOException {
        File inFile = new File(sourcePath);
        File outFile = new File(descPath);
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = new FileInputStream(inFile);
            outputStream = new FileOutputStream(outFile);
            byte[] bytes = new byte[1024];
            int len = 0;
            long start =  System.currentTimeMillis();
            while ((len = inputStream.read(bytes)) != -1){
                outputStream.write(bytes,0,len);
            }
            long end = System.currentTimeMillis();
            System.out.println("拷贝文件所花费的时间:"+(end-start));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        }finally {
            inputStream.close();
            outputStream.close();
        }
        //每次从源文件读取数据同时也向目标文件输出数据
        return true;
    }

}

public class FileCopy {
    public static void main(String[] args) throws IOException {

        String sourcePath = File.separator+"E:"+File.separator
                +"picture"+File.separator+"666"+File.separator+"888"+File.separator+"write.txt";
        String destPath = File.separator+"E:"+File.separator
                +"picture"+File.separator+"666"+File.separator+"888"+File.separator+"test2.txt";
        if (FileCopyUtil.sourceFileIsExists(sourcePath)){
            FileCopyUtil.createParentDir(destPath);
            if (FileCopyUtil.fileCopy(sourcePath,destPath)){
                System.out.println("文件拷贝成功");
            }else {
                System.out.println("文件拷贝失败");
            }
        }else{
            System.out.println("文件不存在");
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值