A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

进班了,可是张孝祥老师的java高新技术的视频还没看完。
把视频都下下来嘛,一解压,结果全部都分开在各自的文件夹,看视频的时候下一个视频跟上一个视频老是断开,不爽。想一个一个文件剪切出来,总共50+个视频,嫌麻烦。
于是想学以致用一下。自己编了个程序,结果运行起来拷贝的爆慢,也不知道是不是自己写错了还是java调用系统资源就是这么慢。大家帮忙看下。
需求:把D:\BaiduYunDownload\java高新里面子文件夹里的avi都拷贝到父目录中来。
  1. package com.setProperty;
  2. import java.io.*;

  3. public class setProperty {
  4.         public static void main(String[] args) {
  5.                 copyFile(new File("D:\\BaiduYunDownload\\java高新"));
  6.         }
  7.         /**
  8.          * 拷贝avi文件的函数
  9.          * @param oldFile
  10.          * @param newFile
  11.          */
  12.         static void copyAvi(File oldFile,File newFile){
  13.                 BufferedInputStream bis=null;
  14.                 BufferedOutputStream bos=null;
  15.                 try {
  16.                         bis=new BufferedInputStream(new FileInputStream(oldFile));
  17.                         bos=new BufferedOutputStream(new FileOutputStream(newFile));
  18.                         int i=0;
  19.                         while((i=bis.read())!=-1){
  20.                                 bos.write(i);
  21.                                 bos.flush();
  22.                         }
  23.                 } catch (Exception e) {
  24.                         e.printStackTrace();
  25.                 } finally {
  26.                         try {
  27.                                 if(bis!=null)bis.close();
  28.                         } catch (Exception e) {
  29.                                 e.printStackTrace();
  30.                         }
  31.                         try {
  32.                                 if(bos!=null)bos.close();
  33.                         } catch (Exception e) {
  34.                                 e.printStackTrace();
  35.                         }
  36.                 }
  37.         }
  38.         /**
  39.          * 判断是否是目录,且子目录里的文件是不是.avi格式的,如果是就拷贝
  40.          * @param oldFile
  41.          */
  42.         static void copyFile(File oldFile) {
  43.                 File[]files=oldFile.listFiles();
  44.                 for (File file : files) {
  45.                         if(file.isDirectory()){//是目录的就执行深度拷贝
  46.                                 File[] f=file.listFiles(new FilenameFilter() {
  47.                                         @Override
  48.                                         public boolean accept(File arg0, String arg1) {
  49.                                                 // TODO 自动生成的方法存根
  50.                                                 return arg1.endsWith(".avi");
  51.                                         }
  52.                                 });
  53.                                 for (File file2 : f) {
  54.                                         copyAvi(file2,new File("D:\\BaiduYunDownload\\java高新\\"+file2.getName()));
  55.                                 }
  56.                         }

  57.                        
  58.                 }
  59.         }
  60. }
复制代码



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马