想把一个文件夹里的图片(这个文件夹里有文件夹也有图片,我想只拷备图片)拷备到另一个文件夹,被拒绝。单纯拷贝文件就没问题。
public class MainClass {
public static void main(String[] args) throws Exception {
findAndCopyPics("C:\\Users\\Administrator\\Desktop\\370to470");
}
public static void findAndCopyPics(String path) throws Exception {
File file = new File(path);
if (!file.isDirectory())
return;
File[] listFiles = file.listFiles();
for (File f : listFiles) {
if (f.isDirectory()) {// 是文件夹就递归
findAndCopyPics(path + "\\" + f.getName());
} else {// 不是文件夹,即是文件就拷备
copyPics(path + "\\" + f.getName());
}
}
}
public static void copyPics(String path) throws Exception {
File inputfile = new File(path);
File outputfile = new File("C:\\axaa\\");// 输出的文件夹.
System.out.println(path);
FileInputStream fis = new FileInputStream(inputfile);
FileOutputStream fos = new FileOutputStream(outputfile);
byte[] b = new byte[1024];
while (fis.read(b) > 0) {
fos.write(b);
}
fis.close();
fos.close();
}
fos.close();
} |