复制指定类型文件

来源:互联网 发布:手机苏戴斯诵读软件 编辑:程序博客网 时间:2024/06/06 09:06

package homeWork;

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

public class Copy_java
{
        /**
         * F:\java\day10
         * 编写一个程序,将某目录下的所有.java文件复制到f:\jad目录下。
         */
        public static void main(String[] args)
        {
                File f = new File("F:\\java\\day10");
               
                //寻找.java文件需要使用到文件过滤器
                String [] files = f.list(new Guolvqi(".java"));
                //得到后需要复制
                //创建读取流对象
                FileInputStream fis = null;
                //创建写入流对象
                FileOutputStream fos = null;
                //创建容器
                byte [] temp = new byte[1024];
                File file = new File("f:\\jad");
                file.mkdir();
//                System.out.println(file.getPath());
                for (String string : files)
                {
                        System.out.println(string);
                        try
                        {
                                //源路径
                                fis = new FileInputStream(f.getPath()+"\\"+string);
                                //复制路径
                                fos = new FileOutputStream(file.getPath()+"\\"+string);
                                while (true)
                                {
                                        //创建接收对象用来判断何时结束
                                        int num = fis.read(temp);
                                        if (num==-1)
                                        {
                                                break;
                                        }
                                        fos.write(temp,0,num);
                                }
                        } catch (FileNotFoundException e)
                        {
                                e.printStackTrace();
                        } catch (IOException e)
                        {
                                e.printStackTrace();
                        }
                        finally{
                                try
                                {
                                        fos.close();
                                        fis.close();
                                } catch (IOException e)
                                {
                                        e.printStackTrace();
                                }
                        }
                }
               
        }
       
}

过滤器

package homeWork;

import java.io.File;
import java.io.FilenameFilter;

public class Guolvqi implements FilenameFilter
{
        private String ext;
       
        public Guolvqi(String ext)
        {
                super();
                this.ext = ext;
        }
        public boolean accept(File dir, String name)
        {
               
                if (dir.exists()&&dir.isDirectory())
                {
                        if (name.endsWith(ext))
                        {
                                return true;
                        }
                        else {
                                return false;
                        }
                }
                return false;
        }

}

0 0