进班了,可是张孝祥老师的java高新技术的视频还没看完。
把视频都下下来嘛,一解压,结果全部都分开在各自的文件夹,看视频的时候下一个视频跟上一个视频老是断开,不爽。想一个一个文件剪切出来,总共50+个视频,嫌麻烦。
于是想学以致用一下。自己编了个程序,结果运行起来拷贝的爆慢,也不知道是不是自己写错了还是java调用系统资源就是这么慢。大家帮忙看下。
需求:把D:\BaiduYunDownload\java高新里面子文件夹里的avi都拷贝到父目录中来。
- package com.setProperty;
- import java.io.*;
- public class setProperty {
- public static void main(String[] args) {
- copyFile(new File("D:\\BaiduYunDownload\\java高新"));
- }
- /**
- * 拷贝avi文件的函数
- * @param oldFile
- * @param newFile
- */
- static void copyAvi(File oldFile,File newFile){
- BufferedInputStream bis=null;
- BufferedOutputStream bos=null;
- try {
- bis=new BufferedInputStream(new FileInputStream(oldFile));
- bos=new BufferedOutputStream(new FileOutputStream(newFile));
- int i=0;
- while((i=bis.read())!=-1){
- bos.write(i);
- bos.flush();
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if(bis!=null)bis.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- if(bos!=null)bos.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 判断是否是目录,且子目录里的文件是不是.avi格式的,如果是就拷贝
- * @param oldFile
- */
- static void copyFile(File oldFile) {
- File[]files=oldFile.listFiles();
- for (File file : files) {
- if(file.isDirectory()){//是目录的就执行深度拷贝
- File[] f=file.listFiles(new FilenameFilter() {
- @Override
- public boolean accept(File arg0, String arg1) {
- // TODO 自动生成的方法存根
- return arg1.endsWith(".avi");
- }
- });
- for (File file2 : f) {
- copyAvi(file2,new File("D:\\BaiduYunDownload\\java高新\\"+file2.getName()));
- }
- }
-
- }
- }
- }
复制代码
|
|