io流 把a文件夹下的文件复制到b文件夹下

package test1;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;

public class Bouy {
public static void main(String[] args) throws IOException {
getCopy("d:/a/","d:/text/langfei/1/2" );// 调用1~2
// 把a文件夹下的文本文件复制到b文件夹下
}
public static void getCopy(String a,String b) throws IOException { //-------------------- 1
File fileb = new File(b);
if(!fileb.isDirectory()) {
fileb.mkdirs();
}
File filea = new File(a);
File[] files = filea.listFiles();
for(File file : files) {
if(file.isFile()) {
BufferedInputStream bif = new BufferedInputStream(new FileInputStream(a+"\\"+file.getName()));
BufferedOutputStream aaa = new BufferedOutputStream(new FileOutputStream(b+"\\"+file.getName()));
byte[] bytes = new byte[1024];
int len=0;
while((len= bif.read(bytes))!=-1) {
aaa.write(bytes,0,len);
}
bif.close();
aaa.close();
}else {
getCopy(a+"\\"+file.getName(), b+"\\"+file.getName());
}

}
} // ------------------------------------------------------------------------2
}

posted @ 2021-08-03 11:25  十万万万个为什么  阅读(130)  评论(0)    收藏  举报