java文件复制(可过滤)
1.java将制定的后缀文件导出到另一个文件中,递归遍历某个文件夹下的所有文件
直接贴上代码
import java.io.*;
public class FileExport {/**
* Created by Administrator on 2016/11/7 0007.
*/
public static void main(String[] args){
String path = "F:\\百度云盘\\Scala";
traverseFolder2(path);
}
public static void traverseFolder2(String path){
File file = new File(path);
if (file.exists()) {
File[] files = file.listFiles();
if (files.length == 0) {
System.out.println("文件夹是空的!");
return;
} else {
for (File file2 : files) {
if (file2.isDirectory()) {
//判断后缀是否是mp4,是的话导出即可
if(file2.getAbsolutePath().contains("mp4"))
{
String tempStr = file2.getAbsolutePath().split("\\\\")[file2.getAbsolutePath().split("\\\\").length - 1];
ExceptionDoing(file2.getAbsolutePath(),"C:\\Users\\Administrator\\Desktop\\scala视频\\" + tempStr);
}
traverseFolder2(file2.getAbsolutePath());
} else {
if(file2.getAbsolutePath().contains("mp4"))
{
String tempStr = file2.getAbsolutePath().split("\\\\")[file2.getAbsolutePath().split("\\\\").length - 1];
ExceptionDoing(file2.getAbsolutePath(),"C:\\Users\\Administrator\\Desktop\\scala视频\\" + tempStr);
}
}
}
}
} else {
System.out.println("文件不存在!");
}
}
public static void ExceptionDoing(String sourceFile,String targetFile){
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile));
byte[] b = new byte[1024];
int len = 0;
while((len = bis.read(b)) != - 1){
bos.write(b,0,len);
bos.flush();
}
bos.close();
bis.close();
}catch(Exception e){
e.printStackTrace();
}
}
}