将500张图片按一定数量存放到不同的文件夹里
import java.io.*;
public class CopyFile{
public static void main(String[] args) {
File file = new File("D:\\BaiduNetdiskDownload\\tx\\壁纸精选500张(三)");
String[] names = file.list();
File[] files = file.listFiles();
assert names != null;
assert files != null;
int dirCount = names.length % 20 == 0 ? names.length / 20 : names.length / 20 + 1;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
for(int i = 0;i < dirCount;i++) {
File tempFile = new File("D:\\BaiduNetdiskDownload\\tx\\壁纸分组\\壁纸分组" + (i + 1));
if (!tempFile.exists()) {
tempFile.mkdirs();
}
FileInputStream fis = null;
FileOutputStream fos = null;
System.out.println("正在操作壁纸分组" + (i + 1));
for(int j = i * 20 ; j < i * 20 + 20 && j < files.length;j++){
System.out.println("正在操作:" + names[j]);
try {
fis = new FileInputStream(files[j]);
fos = new FileOutputStream("D:\\BaiduNetdiskDownload\\tx\\壁纸分组\\壁纸分组" + (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 {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
System.out.println("操作壁纸分组" + (i + 1) + "成功");
}
}
}