黑马程序员技术交流社区
标题:
分享一个查找指定盘的avi视频文件的程序
[打印本页]
作者:
knockout
时间:
2015-9-7 23:36
标题:
分享一个查找指定盘的avi视频文件的程序
//需求:讲一个包含多级目录的文件夹下的所有的avi文件复制到指定的目录,并且改名.MP4
package StreamTest;
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 FileCopyAndRename {
public static void main(String[] args) throws IOException {
// 思路一,先改名,再复制
File src = new File("f:\\");
File dest = new File("D:\\video\\");
if(!dest.exists()){
dest.mkdir();
}
diGuiFiles(src, dest);
}
public static void diGuiFiles(File src, File dest) throws IOException {
if (src != null) {
// 获取目录下所有的文件传入到一个文件数组
File[] filelist = src.listFiles();
if (filelist != null) {
// 遍历并且判断是文件夹还是文件。
for (File file : filelist) {
if (file.isDirectory()) {
diGuiFiles(file, dest);
} else {
String name = file.getName();
if (name.endsWith(".avi")) {
File newFile = new File(dest, name.replace(".avi", ".mp4"));
copys(file, newFile);
}
}
}
}
}
}
// 字节缓冲流,复制文件
public static void copys(File src, File dest) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}
复制代码
这个大家可以改着玩玩
作者:
舒旺
时间:
2015-9-7 23:44
很不错 赞一个
作者:
我来了
时间:
2015-9-7 23:53
貌似能看懂又貌似看不懂
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2