import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws Exception{
//指定目录下所有.avi文件所在的源文件夹。
File src=new File("D:\\编程软件\\黑马训练\\黑马学习视频\\黑马入学前自学视频课程\\Step2—WEB编程基础\\毕向东Html、css、JavaScript视频教程");
File dest=new File("D:\\编程软件\\黑马训练\\黑马学习视频\\黑马入学前自学视频课程\\Step2—WEB编程基础\\自动提取视频");
copyAviFile(src,dest);
}
public static void copyAviFile(File src,File dest) throws Exception{
File[] files=src.listFiles();
for(File file: files){
//如果该路径对象是文件夹目录,则进行递归。
if(file.isDirectory()){
copyAviFile(file,dest);
}
else {
if(file.getName().endsWith(".avi")){
}
copy(file,dest);
}
}
}
public static void copy(File src,File dest){
BufferedInputStream bufis= null;
BufferedOutputStream bufos=null;
try{
bufis=new BufferedInputStream(new FileInputStream(src));
bufos=new BufferedOutputStream(new FileOutputStream(dest));
}catch(IOException e){
System.out.println("读写失败,复制出现异常");
}
byte[] buf = new byte[1024*1024];
int len=0;
try{
while((len=bufis.read(buf))!=-1)
{
bufos.write(buf,0,len);
}
}catch(IOException e)
{
}
finally {
try {
if(bufis != null)
bufis.close();
}
catch (IOException e1)
{
throw new RuntimeException("读取关闭失败!");
}
try {
if(bufos!= null)
bufos.close();
}
catch (IOException e2)
{
throw new RuntimeException("写入关闭失败!");
}
}
}
} |