黑马程序员技术交流社区
标题:
Io流的复制问题
[打印本页]
作者:
天涯追梦
时间:
2014-5-12 18:26
标题:
Io流的复制问题
需求:把e盘中文件夹aaa中(包含子文件夹)所有.avi文件,全部复制到a文件中,我写了以下代码,为什么不对呀。请高手解答,谢谢!
package day1;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyAvi {
public static void main(String[] args) throws IOException {
File file = new File("e:\\aaa");
// TODO Auto-generated method stub
File files = new File("e:\\a");
FileFilter ff = new FileFilter() {
@Override
public boolean accept(File pathname) {
// TODO Auto-generated method stub
return pathname.getName().endsWith(".avi");
}
};
copyavi(file,files,ff);
}
private static void copyavi(File file, File files, FileFilter ff) throws IOException {
File [] fi = file.listFiles();
for (File fil : fi) {
if (fil.isDirectory()) {
copyavi(fil,files,ff);
}
else{
if(ff.accept(fil)){
FileInputStream fis = new FileInputStream(fil);
FileOutputStream fos = new FileOutputStream(files);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte buf [] = new byte[1024];
int len = 0;
while ((len = bis.read(buf)) != -1) {
bos.write(buf);
}
bis.close();
bos.close();}
}
}
}
}
复制代码
作者:
天涯追梦
时间:
2014-5-12 18:53
把上面的代码,修改了一下,加上了集合,可是,还是不对呀:(
package day1;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CopyAvi {
public static void main(String[] args) throws IOException {
File file = new File("e:\\aaa");
// TODO Auto-generated method stub
File files = new File("e:\\a");
if (!files.exists()) {
files.mkdirs();
}
List <File>list = new ArrayList<File>();
FileFilter ff = new FileFilter() {
@Override
public boolean accept(File pathname) {
// TODO Auto-generated method stub
return pathname.getName().endsWith(".java");
}
};
copyavi(file,list,ff);
for (File file2 : list) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file2));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(files));
byte buf [] = new byte[1024];
int len = 0;
while ((len = bis.read(buf)) != -1) {
bos.write(buf,0,len);
}
bis.close();
bos.close();
}
}
private static void copyavi(File file, List list, FileFilter ff) throws IOException {
File [] fi = file.listFiles();
for (File fil : fi) {
if (fil.isDirectory()) {
copyavi(fil,list,ff);
}
else{
if(ff.accept(fil)){
list.add(fil);
}
}
}
}
}
复制代码
作者:
月光海
时间:
2014-5-12 19:29
代码小改了一下,可复制,但是不能原样复制
package day1;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CopyAvi {
public static void main(String[] args) throws IOException {
File file = new File("e:\\aaa");
// TODO Auto-generated method stub
File files = new File("e:\\a");
if (!files.exists()) {
files.mkdirs();
}
List<File> list = new ArrayList<File>();
FileFilter ff = new FileFilter() {
@Override
public boolean accept(File pathname) {
// TODO Auto-generated method stub
return pathname.getName().endsWith(".avi");
}
};
copyavi(file, list, ff);
for (File file2 : list) {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file2));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(files.getAbsolutePath()+File.separator+file2.getName()));//看这里
byte buf[] = new byte[1024];
int len = 0;
while ((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len);
}
bis.close();
bos.close();
}
}
private static void copyavi(File file, List list, FileFilter ff)
throws IOException {
File[] fi = file.listFiles();
for (File fil : fi) {
if (fil.isDirectory()) {
copyavi(fil, list, ff);
} else {
if (ff.accept(fil)) {
list.add(fil);
}
}
}
}
}
复制代码
作者:
Yov正
时间:
2014-5-12 19:40
File files = new File("e:\\a\\");
files.mkdir();
复制代码
eles里面开头添加
File fileout=new File(files,fil.getName());
复制代码
修改此行
FileOutputStream fos = new FileOutputStream(fileout);
复制代码
作者:
天涯追梦
时间:
2014-5-12 21:43
总结一下:
需求:在看张孝祥老师的高新技术视频时文件夹太多,看视频需要一个个点开文件夹,所以我想的是把所有文件夹的视频文件集中在一个文件夹中,所以写了以上代码,结果出现了问题。经过一些尝试和上面两位朋友的帮助,成功的完成了需求。
下面是完整代码,与大家分享。
//将指定文件夹中(包含子文件夹的)所有指定类型的文件复制到某一文件夹中
package day1;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CopyAvi {
public static void main(String[] args) throws IOException {
File file = new File("G:\\学习视频\\张孝祥2010年贺岁视频:Java高新技术");
// TODO Auto-generated method stub
File files = new File("G:\\学习视频\\张孝祥2010年贺岁视频:avi");
if (!files.exists()) {
files.mkdirs();
}
List <File>list = new ArrayList<File>();
FileFilter ff = new FileFilter() {
@Override
public boolean accept(File pathname) {
// TODO Auto-generated method stub
return pathname.getName().endsWith(".avi");
}
};
copyavi(file,list,ff);
for (File file2 : list) {
String str = file2.getName();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file2));
File fill = new File(files,str);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fill));
byte buf [] = new byte[1024*1024];
int len = 0;
while ((len = bis.read(buf)) != -1) {
bos.write(buf,0,len);
}
bis.close();
bos.close();
}
}
private static void copyavi(File file, List list, FileFilter ff) throws IOException {
File [] fi = file.listFiles();
for (File fil : fi) {
if (fil.isDirectory()) {
copyavi(fil,list,ff);
}
else{
if(ff.accept(fil)){
list.add(fil);
}
}
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2