A文件夹下面有多个子文件夹,而后子文件下后面有一些.jpg文件,要求把这些.jpg文件彻底拷贝复制到B文件夹。java
先遍历循环A文件夹下的文件,而后找到符合.jpg的文件,放到一个列表中,而后再把列表中的jpg文件放到B文件夹上。code
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.file.Files; import java.util.LinkedList; import java.util.List; import java.util.Queue; public class Picture { private static LinkedList<File> fileQueue= new LinkedList<>(); private final String aPath="C:\\Users\\m088163\\Pictures"; private final String bPath="D:\\temp"; /** * 将源文件夹下全部的.jpg文件找到,并存在fileQueue 列表中。 * @param path 须要查找文件的路径名 * @param subStr 匹配的文件类型 */ public void FindJpg(String path,String subStr){ File file=new File(path); File[] files=file.listFiles(); for(int i=0;i<files.length;i++){ if(files[i].isDirectory()){ FindJpg(files[i].getAbsolutePath(),subStr); } if(files[i].getAbsolutePath().contains(subStr)){ fileQueue.add(files[i]); } } } /** * 将fileQeueue中的jpg文件存在目标文件夹。 * @param path * @throws Exception */ public void moveJpg(String path) throws Exception{ String myPath=""; File newFile; for(File files:fileQueue){ myPath=path+"\\"; myPath+=files.getName(); System.out.println(files.getName()); newFile=new File(myPath); if(newFile.exists()){ System.out.println("建立文件失败"+newFile+"失败,目标文件已经存在"); } if(!newFile.getParentFile().exists()){ System.out.println("文件的根目录不存在,建立根目录"); newFile.getParentFile().mkdir(); } copyfile(files.getAbsoluteFile(),newFile); } } /** * 写入文件操做 * @param fromDest * @param toDest * @throws Exception */ public void copyfile(File fromDest,File toDest ) throws Exception{ FileInputStream is =new FileInputStream(fromDest); FileOutputStream os =new FileOutputStream(toDest); byte[] b =new byte[1024]; int temp=0; while((temp=is.read(b))!=-1){ os.write(b,0,temp); } is.close(); os.close(); } public static void main(String[] args) { Picture picture = new Picture(); picture.FindJpg(picture.aPath, ".jpg"); try { picture.moveJpg(picture.bPath); } catch (Exception e) { e.printStackTrace(); } } } import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.file.Files; import java.util.LinkedList; import java.util.List; import java.util.Queue; public class Picture { private static LinkedList<File> fileQueue= new LinkedList<>(); private final String aPath="C:\\Users\\m088163\\Pictures"; private final String bPath="D:\\temp"; /** * 将源文件夹下全部的.jpg文件找到,并存在fileQueue 列表中。 * @param path 须要查找文件的路径名 * @param subStr 匹配的文件类型 */ public void FindJpg(String path,String subStr){ File file=new File(path); File[] files=file.listFiles(); for(int i=0;i<files.length;i++){ if(files[i].isDirectory()){ FindJpg(files[i].getAbsolutePath(),subStr); } if(files[i].getAbsolutePath().contains(subStr)){ fileQueue.add(files[i]); } } } /** * 将fileQeueue中的jpg文件存在目标文件夹。 * @param path * @throws Exception */ public void moveJpg(String path) throws Exception{ String myPath=""; File newFile; for(File files:fileQueue){ myPath=path+"\\"; myPath+=files.getName(); System.out.println(files.getName()); newFile=new File(myPath); if(newFile.exists()){ System.out.println("建立文件失败"+newFile+"失败,目标文件已经存在"); } if(!newFile.getParentFile().exists()){ System.out.println("文件的根目录不存在,建立根目录"); newFile.getParentFile().mkdir(); } copyfile(files.getAbsoluteFile(),newFile); } } /** * 写入文件操做 * @param fromDest * @param toDest * @throws Exception */ public void copyfile(File fromDest,File toDest ) throws Exception{ FileInputStream is =new FileInputStream(fromDest); FileOutputStream os =new FileOutputStream(toDest); byte[] b =new byte[1024]; int temp=0; while((temp=is.read(b))!=-1){ os.write(b,0,temp); } is.close(); os.close(); } public static void main(String[] args) { Picture picture = new Picture(); picture.FindJpg(picture.aPath, ".jpg"); try { picture.moveJpg(picture.bPath); } catch (Exception e) { e.printStackTrace(); } } }