Java---几种常用的文件复制方法

复制单级文件夹,复制过去后,把原文夹里面的 .jpg文件,后缀名改成 .png
public class CopyFileDemo {
    public static void main(String[] args) throws IOException {
        //复制单级文件夹并该后缀名
        //1.封装源文件夹
        File srcFolder = new File("D:\\test");
        //2.封装目标文件夹
        File targetFolder = new File("E:\\test");
        if (!targetFolder.exists()) {
            targetFolder.mkdirs();
        }
        //复制文件夹
        copyFolder(srcFolder, targetFolder);

        System.out.println("复制完成!");
    }

    private static void copyFolder(File srcFolder, File targetFolder) throws IOException {
        //获取源文件夹下所有的文件,复制并改名
        File[] files = srcFolder.listFiles();
        for (File f : files) {
            copyFiles(f, targetFolder);
        }
    }

    private static void copyFiles(File f, File targetFolder) throws IOException {
        // System.out.println(targetFolder + "\\" + f.getName());
        //读取源文件
        FileInputStream in = new FileInputStream(f);
        FileOutputStream out = null;
        if (f.getName().endsWith(".jpg")) {
            String newName = f.getName().substring(0, f.getName().lastIndexOf("."));
            out = new FileOutputStream(targetFolder + "\\" + newName + ".png");
        } else {
            out = new FileOutputStream(targetFolder + "\\" + f.getName());
        }

        byte[] bytes = new byte[1024 * 8];
        int len = 0;
        while ((len = in.read(bytes)) != -1) {
            out.write(bytes, 0, len);
            out.flush();
        }
        in.close();
        out.close();
    }
}


复制多级文件夹
public class CopyFile {
    public static void main(String[] args) throws IOException {
        File oldfile = new File("c:\\0417");
        File newfile = new File("c:\\新建文件夹\\3");
        if (newfile.exists()) {
            System.out.println("文件名冲突");
            return;
        } else {
            newfile.mkdirs();
        }
        CopyFiles(oldfile, newfile);
    }

    private static void CopyFiles(File oldfile, File newfile) throws IOException {

        File[] files = oldfile.listFiles();
        for (File file1 : files) {
            if (file1.isDirectory()) {
                File file = new File(newfile + "\\" + file1.getName());
                file.mkdirs();
                System.out.println(file.getName());
                CopyFiles(file1, file);
            } else {
                copysinglefile(file1,newfile);
            }

        }

private static void copysinglefile(File oldfile, File newfile) throws IOException {
        //FileOutputStream out = new FileOutputStream(oldfile);
        // FileoutputStream in = new FileOutputStream(newfile+"\\"+oldfile.getName());

        FileInputStream in = new FileInputStream(oldfile);
        FileOutputStream out = new FileOutputStream(newfile + "\\" + oldfile.getName());

        byte[] bytes = new byte[1024 * 8];
        int len = 0;
        while ((len = in.read(bytes)) != -1) {
            out.write(bytes, 0, len);
        }
        in.close();
        out.close();


    }
}


复制多级文件夹,复制过去后,把原文夹里面的 .jpg文件,后缀名改成 .png


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class copyrename2 {
    public static void main(String[] args) throws IOException {
        File oldfile = new File("c:\\0417");
        File newfile = new File("c:\\新建文件夹\\8");
            newfile.mkdirs();
        CopyFiles(oldfile,newfile);
}

    private static void CopyFiles(File oldfile, File newfile) throws IOException {

        File[] files = oldfile.listFiles();
        for (File file1 : files) {
            if (file1.isDirectory()){
            //如果是文件夹,就创建新的文件夹,并进行递归遍历文件夹内部的内容
                File file = new File(newfile + "\\" + file1.getName());
                file.mkdirs();
                CopyFiles(file1,file);
            } else if (file1.getName().endsWith(".jpg")){
            //获取源文件名字
               String s=file1.getName();
               //更改后缀名
               s=s.substring(0,s.lastIndexOf(".")).concat(".png");
                FileOutputStream out = new FileOutputStream( new File(newfile+"\\"+s));
                copysinglefile(file1,out);
            }else {
                FileOutputStream out = new FileOutputStream(newfile + "\\" + file1.getName());
                copysinglefile(file1,out);
            }

        }
    }

    private static void copysinglefile(File oldfile, FileOutputStream out) throws IOException {
              FileInputStream in = new FileInputStream(oldfile);

        byte[] bytes = new byte[1024 * 8];
        int len=0;
        while ((len=in.read(bytes))!=-1){
          out.write(bytes,0,len);
        }
        in.close();
        out.close();


    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值