* 题目三:把指定目录下(包含子目录)的所有图片,复制到另一个指定目录下package com.heima.exercise;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;public class num03 { /** * @throws IOException */ public static void main(String[] args) throws IOException { File file = new File("D:\\测试1"); File file2 = new File("D:\\测试2"); ArrayList<File> newList = new ArrayList<>(); getFiles(newList,file); BufferedInputStream bis = null; BufferedOutputStream bos = null; for (File file3 : newList) { bis = new BufferedInputStream(new FileInputStream(file3)); bos = new BufferedOutputStream(new FileOutputStream(new File(file2,file3.getName()))); int b; while ((b=bis.read()) != -1) { bos.write(b); } } bis.close(); bos.close(); } private static void getFiles(ArrayList<File> list,File file) { File[] arr = file.listFiles(); for (File file2 : arr) { if (file2.isDirectory()) { getFiles(list,file2); }else if( file2.isFile() && (file2.getName().endsWith(".jpg")|| file2.getName().endsWith(".bmp")||file2.getName().endsWith(".png"))) { list.add(file2); } } }}
|