zoukankan      html  css  js  c++  java
  • Java_从指定的目录下拷贝所有指定类型文件到output目录

     1 import java.io.*;
     2 
     3 public class MainCopy {
     4  
     5     private int directoryCount = 0;
     6     private int fileCount = 0;
     7     private int replayCount = 0;
     8     private int alreadyCount = 0;
     9  
    10     public void findFile(String inPath, final String outPath) {
    11         File file = new File(inPath);
    12         if (file.isDirectory()) {
    13             directoryCount++;
    14             File[] files = file.listFiles();
    15             for (final File f : files) {
    16                 if (f.isDirectory()) {
    17                     directoryCount++;
    18                     new Thread() {
    19                         public void run() {
    20                         }
    21                     }.start();
    22                     findFile(f.getPath(), outPath);
    23                 }
    24                 if (f.isFile()) {
    25                     fileCount++;
    26                     if (f.getName().endsWith(".mp3")) {
    27                         File newFile = new File(outPath + "/" + f.getName());
    28                         if (newFile.exists()) {
    29                             replayCount++;
    30                             System.out.println("file is exist" + f.getPath());
    31                         } else {
    32                             synchronized (newFile) {
    33                                 writeToFile(f, newFile);
    34                             }
    35                             alreadyCount++;
    36                             System.out.println("copy :" + f.getPath());
    37                         }
    38                     } else {
    39                         System.out
    40                                 .println("---------------------------------------other file type:"
    41                                         + f.getName());
    42                     }
    43                 }
    44             }
    45         }
    46     }
    47     public void writeToFile(File in, File out) {
    48         FileInputStream fis = null;
    49         FileOutputStream fos = null;
    50         try {
    51             fis = new FileInputStream(in);
    52             fos = new FileOutputStream(out);
    53             byte[] buffer = new byte[1024];
    54             int len = 0;
    55             while ((len = fis.read(buffer)) > 0) {
    56                 fos.write(buffer, 0, len);
    57             }
    58             fos.close();
    59             fis.close();
    60         } catch (FileNotFoundException e) {
    61             e.printStackTrace();
    62         } catch (IOException e) {
    63             e.printStackTrace();
    64         }
    65     }
    66     public static void main(String[] args) {
    67         MainCopy c = new MainCopy();
    68         long startTime = System.currentTimeMillis();
    69         c.findFile("D:/31-60", "D:/E_pod");
    70         System.out.println(c.directoryCount);
    71         System.out.println(c.fileCount);
    72         System.out.println(c.alreadyCount);
    73         long endTime = System.currentTimeMillis();
    74         System.out.println("time count:"+(endTime-startTime)+"ms");
    75     }
    76 }
    import java.io.*;
    
    public class MainCopy {
     
        private int directoryCount = 0;
        private int fileCount = 0;
        private int replayCount = 0;
        private int alreadyCount = 0;
     
        public void findFile(String inPath, final String outPath) {
            File file = new File(inPath);
            if (file.isDirectory()) {
                directoryCount++;
                File[] files = file.listFiles();
                for (final File f : files) {
                    if (f.isDirectory()) {
                        directoryCount++;
                        new Thread() {
                            public void run() {
                            }
                        }.start();
                        findFile(f.getPath(), outPath);
                    }
                    if (f.isFile()) {
                        fileCount++;
                        if (f.getName().endsWith(".mp3")) {
                            File newFile = new File(outPath + "/" + f.getName());
                            if (newFile.exists()) {
                                replayCount++;
                                System.out.println("file is exist" + f.getPath());
                            } else {
                                synchronized (newFile) {
                                    writeToFile(f, newFile);
                                }
                                alreadyCount++;
                                System.out.println("copy :" + f.getPath());
                            }
                        } else {
                            System.out
                                    .println("---------------------------------------other file type:"
                                            + f.getName());
                        }
                    }
                }
            }
        }
        public void writeToFile(File in, File out) {
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                fis = new FileInputStream(in);
                fos = new FileOutputStream(out);
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = fis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                fis.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public static void main(String[] args) {
            MainCopy c = new MainCopy();
            long startTime = System.currentTimeMillis();
            c.findFile("D:/31-60", "D:/E_pod");
            System.out.println(c.directoryCount);
            System.out.println(c.fileCount);
            System.out.println(c.alreadyCount);
            long endTime = System.currentTimeMillis();
            System.out.println("time count:"+(endTime-startTime)+"ms");
        }
    }
  • 相关阅读:
    IHttpHandler做文件防盗链接
    使用SQL的全文搜索功能构建 Web 搜索应用程序
    Google 二维条码 API
    Flash Media Server 入门教程
    Janus WinForms Controls Suite v2.0.1000
    如何将数据大容量加载到合并发布中的表(复制 TransactSQL 编程)
    转:CentOS 5.4下的Memcache安装
    转:用php读取xml数据
    HPUX启动和关闭
    目录,文件操作详谈—php
  • 原文地址:https://www.cnblogs.com/charles999/p/6961352.html
Copyright © 2011-2022 走看看