package md5;
import java.io.*;
/**
* @date 2021/02/23
*/
public class Test{
public static void main(String[] args) {
File file = new File("E:\\手机照片\\2022-10-10手机照片");
// 获得图片名列表
String[] names = file.list();
// 获得图片列表
File[] files = file.listFiles();
assert names != null;
assert files != null;
// 需要创建的文件夹数量,每个文件夹放500张图片
int dirCount = names.length % 500 == 0 ? names.length / 500 : names.length / 500 + 1;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
for(int i = 0;i < dirCount;i++) {
File tempFile = new File("E:\\手机照片\\2022-10-10手机照片\\图片分组" + (i + 1));
if (!tempFile.exists()) {
tempFile.mkdirs();
}
FileInputStream fis = null;
FileOutputStream fos = null;
System.out.println("正在操作壁纸分组" + (i + 1));
for(int j = i * 500 ; j < i * 500 + 500 && j < files.length;j++){
System.out.println("正在操作:" + names[j]);
try {
fis = new FileInputStream(files[j]);
fos = new FileOutputStream("E:\\手机照片\\2022-10-10手机照片\\图片分组" + (i + 1)
+"\\" + names[j]);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] buffer = new byte[1024];
int len;
while((len = bis.read(buffer)) != -1){
bos.write(buffer,0,len);
bos.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(bis != null){
try {
// 外层流关闭内层流自动关闭,所有不需要fis.close();
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bos != null){
try {
// 外层流关闭内层流自动关闭,所有不需要fos.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
System.out.println("操作图片分组" + (i + 1) + "成功");
}
}
}
将大量图片分组,分别存放不同的文件夹里
最新推荐文章于 2024-07-22 02:47:18 发布