package reviw1;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
public class asdf {
public static void main(String[] args) throws IOException {
File file=new File("C:\\Users\\huang\\Desktop\\JAVA\\day26");
BufferedInputStream bufis=null;
BufferedOutputStream bufou=null;
int len=0;
byte [] buf=new byte[1024];
String dir="D:\\资料\\新建文件夹";
File[] files=file.listFiles(new MyFilenameFilter1(".java"));
for(int x=0;x<files.length;x++)
{
bufis=new BufferedInputStream(new FileInputStream(files[x]));
String[] fileName=files[x].getName().split("\\.");
while((len=bufis.read(buf))!=-1)
{
bufou=new BufferedOutputStream(new FileOutputStream(dir+"\\"+fileName[0]+".txt"));
bufou.write(buf,0,len);
bufou.flush();
}
}
bufou.close();
bufis.close();
}
}
class MyFilenameFilter1 implements FilenameFilter
{
String type;
public MyFilenameFilter1(String type){
this.type=type;
}
public boolean accept(File dir, String name) {
return name.endsWith(type);
}
}
如题,听说很多朋友面试都遇到了复制文件的题目,我就写了一个练习一下,但是发现有的文件完全复制了,有的复制了一点点...好奇怪,我文件夹下有12个文件,全都复制过去了,但是只有6-7个是完全复制好的,还有部分都是复制了一点点! |
|