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(); } } }