Java进阶:拷贝相关实现

拷贝文件

/**
 * 使用FileInputStream + FileOutputStream完成文件拷贝
 * 使用以上的字节流拷贝文件的时候,不用在意文件类型。任意文件类型都能拷贝。
 *
 * @author lwx
 * @since  20220408
 */
public class CopyTest {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            // 创建输入流对象
            fis = new FileInputStream("E:/Users/DIY/IdeaProjects/JavaSE/567/temp1");
            // 创建输出流对象
            fos = new FileOutputStream("E:/Users/DIY/IdeaProjects/JavaSE/567/temp2");

            // 一边读一边写
            byte[] bytes = new byte[1024 * 1024];   // 1MB(一次最多拷贝1MB)
            int readCount = 0;
            while ((readCount = fis.read(bytes)) != -1){
                fos.write(bytes, 0, readCount);
            }
            // 刷新
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

拷贝普通文本

/**
 * 使用 FileReader + FileWriter 进行拷贝。
 * 只能拷贝普通文本。
 *
 * @author lwx
 * @since  20220408
 */
public class CopyTest {
    public static void main(String[] args) {
        FileReader reader = null;
        FileWriter writer = null;
        try {
            // 创建文件字符输入流
            reader = new FileReader("E:/Users/DIY/IdeaProjects/JavaSE/567/File.java");
            // 创建文件字符输出流
            writer = new FileWriter("E:/Users/DIY/IdeaProjects/JavaSE/567/File1.java");

            char[] chars = new char[1024 * 512];    //1MB
            int readCount = 0;
            while ((readCount = reader.read(chars)) != -1) {
                writer.write(chars,0,readCount);
            }

            // 刷新
            writer.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

拷贝目录

/**
 * 拷贝目录下所有文件
 * 使用File + FileInputStream + FileOutputStream完成拷贝
 * 
 * @author lwx
 * @since  20220412
 */
public class CopyAll {
    public static void main(String[] args) {
        // 拷贝源
        File srcFile = new File("E:\\a");
        // 拷贝目标
        File destFile = new File("E:\\file");
        // 调用拷贝方法
        copyDir(srcFile, destFile);
    }

    /**
     * 拷贝目录
     * @param srcFile 拷贝源
     * @param destFile 拷贝目标
     */
    private static void copyDir(File srcFile, File destFile) {
        FileInputStream fis = null;
        FileOutputStream fos = null;

        // 获取当前目录下所有的子文件
        File[] files = srcFile.listFiles();

        for (File file : files) {
            if (file.isDirectory()){    //如果是目录
                //System.out.println(file.getPath());
                // 源路径
                String srcDir = file.getAbsolutePath();
                // 目标路径
                String destDir = destFile.getAbsolutePath() + srcDir.substring(2);
                File file1 = new File(destDir);
                if (!file1.exists()) {  //如果路径不存在
                    file1.mkdirs();
                }
                // 递归调用
                copyDir(file, destFile);
            }else if (file.isFile()) { //如果是文件
                //System.out.println(file.getPath());
                try {
                    // 创建输入流对象
                    fis = new FileInputStream(file.getAbsolutePath());
                    // 创建输出流对象
                    fos = new FileOutputStream(destFile.getAbsolutePath() + file.getAbsolutePath().substring(2));

                    // 一边读一边写
                    byte[] bytes = new byte[1024 * 1024];   // 1MB(一次最多拷贝1MB)
                    int readCount = 0;
                    while ((readCount = fis.read(bytes)) != -1){
                        fos.write(bytes, 0, readCount);
                    }

                    // 刷新
                    fos.flush();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            //System.out.println(file.getPath());
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值