批量复制照片

实现批量复制图片

最近得到一个充满照片的压缩文件,想把他们作为屏保(幻灯片放映的那种),但是他们都在一个大文件下的子文件下的子文件下,就是下图这样的,幻灯片放映效果是所有图片在一个文件夹下,一个个复制也忒麻烦,所以就想到用写了个小程序(java)来解决问题.
批量复制照片_第1张图片

具体代码如下:

import java.io.*;

/**
 * 此类是用来递归的复制文件下的照片的
 * @author Administrator
 *
 */
public class CopyPhoto {
	public static void copyPhoto(File oldFile) throws Exception//oldFile是要复制的那个文件夹
	{
		File[] files=oldFile.listFiles();//这是将oldFile文件夹下的一级文件放到数组中
		for (File file : files) {
			if(file.isDirectory())//判断这个一级文件是不是一个目录
			{
				copyPhoto(file);//是的话递归执行copyPhoto方法
			}else{
				File newFile=new File("D:\\copyTest12");//将要将复制好的文件放到那个文件夹下
				//下面用的是字节流读取的方法,字节流是无法复制照片文件的
				//因为该文件夹下都是图片,所以可以直接复制,如果遇到有文件有图片的可以写一个正则表达式来匹配.
				BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
				BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(newFile+"\\"+file.getName()));
				byte[] buf=new byte[1024*20];
				//int a=0;
				int length=0;
				while((length=bis.read(buf))!=-1)
				{
					bos.write(buf,0,length);
				}
				bos.close();
				bis.close();
			}
		}
		
	}
	public static void main(String[] args)  {
		try {
			File oldFile=new File("d:\\javaIO");
			copyPhoto(oldFile);
			System.out.println("复制完毕");
		} catch (Exception e) {
			
		}	
	}
}

你可能感兴趣的:(java)