package com.hp.jwq; import java.io.*; /** * 批量上传相册 * 将一个文件夹下的所有图片,复制到另一个文件夹 * 仅给出文件夹目录 */ public class Jwq { public static void main(String[] args) { File file1 = new File("D:\\焦"); File file2 = new File("D:\\新建文件夹"); copy(file1, file2); } public static void copy(File file1,File file2){ //获取file1子文件 File[] imgs = file1.listFiles(); //遍历,每次循环都是一个要复制的文件 for (int i = 0; i < imgs.length; i++) { //System.out.println(imgs[i]); //通过io流把文件边读边写,复制到file2目录中 try { FileInputStream in = new FileInputStream(imgs[i]); File imgFile = new File(file2,imgs[i].getName()); //System.out.println("复制到img2文件夹下的图片的路径="+imgFile); FileOutputStream out = new FileOutputStream(imgFile); byte[] bytes=new byte[1024]; int len = 0; while ((len=in.read(bytes))!=-1){ out.write(bytes,0,len); } out.close(); in.close(); System.out.println("图片["+imgs[i].getName()+"]打印完毕"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }
09-22
213
08-09