package charpter11;
import java.io.*;
public class CopyHome {
public static void main(String[] args) {
String dir="C:\\Users\\asus\\Desktop\\ceshi\\test";
File file=new File(dir+"\\img1");
File[] filelist=file.listFiles();
for(File from:filelist){
String name=from.getName();
copy(from.getAbsolutePath(),dir+"\\img2\\"+name);
}
}
public static void copy(String from ,String to){
System.out.println(from +" "+to);
try {
//通过new File(to).getParent()得到上一层路径地址,建立File类
File file=new File(new File(to).getParent());
//如果不存在,就创建这个多级目录
if(!file.exists()){
file.mkdirs();
}
FileInputStream fis=new FileInputStream(from);
BufferedInputStream bis=new BufferedInputStream(fis);
FileOutputStream fos=new FileOutputStream(to);
BufferedOutputStream bos=new BufferedOutputStream(fos);
int size;
byte[] buff=new byte[1024];
while ((size=bis.read(buff))!=-1){
bos.write(buff,0,size);
}
bos.close();
bis.close();
}catch (Exception e){
System.out.println("出现问题");
}
}
}
BufferedInputStream、BufferedOutputStream的图片复制
最新推荐文章于 2022-08-02 09:40:52 发布