将指定目录下所有的固定后缀的文件拷贝到另一个目录

本文介绍了一个Java程序,该程序能够遍历指定目录及其子目录,筛选出所有的JPEG图片文件,并将这些文件复制到另一个指定的位置。通过递归的方式处理文件夹结构,实现了对特定类型文件的有效管理和操作。

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class GetSpecFiles implements FileFilter
{
   
    public void getFiles(String location) 
    {
       
        File file =new File(location);
        File []files =file.listFiles();
        for(File f :files)
        {
            if(f.isDirectory())
            {
                getFiles(f.getPath());
            }
            else
            {
                if(accept(f))
                {
                    try
                    {
                        this.copyFile(f.getPath(), "F:/files/", f.getName());
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
   
    /**
     * @param args
     */
    public static void main(String[] args)
    {
        GetSpecFiles gsf=new GetSpecFiles();
        long time1 = System.currentTimeMillis();
        gsf.getFiles("E:/");
        long time2 = System.currentTimeMillis();
        System.out.print((time2-time1)/(3600*1000));

    }

    @Override
    public boolean accept(File file)
    {
        try
        {

            String[] fileName = file.getName().split("//.");

            if (fileName[1].equalsIgnoreCase("jpg"))
            {
                return true;
            }
            else
            {

                return false;
            }
        }
        catch (Exception e)
        {
            return false;
        }
    }
   
    public  void copyFile(String srcFileName, String destFileDirName,
            String destFileName) throws IOException {
        File srcFile = new File(srcFileName);
        File destFileDir = new File(destFileDirName);
        File destFile = new File(destFileDirName + destFileName);
        FileOutputStream fos = null;
        FileInputStream fis = null;

        //if the source file is empty.
        if (0 == srcFile.length()) {

        }

        //if target directory dosen't exist, make it.
        if (!destFileDir.exists()) {
            if (!destFileDir.mkdirs()) {
            }
        }

        try {
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            byte[] buffer = new byte[1024];
            int i = 0;

            //copy content from source file to target file.
            while ((i = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, i);
                fos.flush();
            }
        } catch (FileNotFoundException e) {
            ;
        } finally {
            IOUtil.close(fis);
            IOUtil.close(fos);
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ybcwjj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值