版权声明:本文为博主原创文章,转载必须注明出处。https://blog.csdn.net/beidouxin2010wang/article/details/83190328
import java.io.*;
public class PicDeal {
//原始图片文件夹
static String originalPath = "D:\\1";
//复制文件地址
static String copyPath = "D:\\2";
//复制图片的方法
private static void copyPhoto(File file) throws IOException {
//将路径封装成File数组
File[] files = file.listFiles();
int j = 0;
//遍历这个数组
for (int i = 0; i<files.length; i++) {
//判断是否是文件夹
if (files[i].isDirectory()) {
//是的话就继续调用这个方法
copyPhoto(files[i]);
} else { //不是的话就匹配是否是图片
//每100张照片生成一个文件夹
if(i%100 == 0) {
j++;
//生成文件夹
File file2 = new File(copyPath+"\\"+j);
file2.mkdir();
} //创建输入文件字节流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(files[i]));
byte[] buf = new byte[1024 * 20];
int lenght = 0;
//创建输出文件字节流包含文件输出位置
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
copyPath +"\\"+j+"\\"+ files[i].getName()));
while ((lenght = bis.read(buf)) != -1) {
bos.write(buf, 0, lenght);
//这一步很重要,没有它图片容易损毁
bos.flush();
if(i == files.length-1) {
bos.close();
bis.close();
System.out.println("#############复制完成!!!#################");
return;
}
}
}
}
}
public static void main(String[] args) {
//需要查找的文件夹
File file = new File(originalPath);
try {
copyPhoto(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}