1.深度遍历指定目录rootPath中所有的子文件、子目录
2.将所有图片(后缀名为*.png\*.jpg)复制到指定目录backupPath
3.返回本次备份的图片数量
/*
* 图片备份
*/
public int backupImage(String rootPath,String backupPath){
return 0;
}
实现思路:
1.定义主函数和备份函数:
main 函数是程序的入口,它调用 backupImage 函数,并打印出备份的图片数量。
backupImage 函数接收两个参数:源路径 rootpath 和备份路径 backupPath。它返回一个整数,表示备份的图片数量。
2.遍历目录:
使用 Files.walkFileTree 方法遍历 rootpath 下的所有文件和子目录。
为此,创建了一个 SimpleFileVisitor 的匿名子类,并重写了其中的 visitFile 方法。这个方法会在遍历到每个文件时被调用。
3.检查文件类型:
在 visitFile 方法中,首先检查文件的名称是否以 .jpg、.png 或 .webp 结尾,以判断它是否是一个图片文件。
4.备份图片:
如果文件是图片,就创建一个新的 File 对象 backImgFile,表示备份图片在 backupPath 下的路径。
使用 BufferedInputStream 和 BufferedOutputStream 来读取源文件并写入备份文件。这里使用了1024字节的缓冲区来读取和写入数据,以提高效率。
备份完成后,counter 变量加1,表示备份的图片数量增加。
另一种备份图片的方式,即使用 Files.copy 方法直接复制文件。但当前实现选择了手动读取和写入的方式。
5.返回备份数量:使用backupImage 函数返回 counter 的值,即备份的图片数量。
代码实现:
package com.ztt.Demo01;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;public class demo15 {public static void main(String[] args) {int ret=backupImage("D:\\test","D:\\test\\img_back");System.out.println(ret);}static int counter=0;public static int backupImage(String rootpath,String backupPath) {try {Files.walkFileTree(Paths.get(rootpath), new SimpleFileVisitor<Path>() {@Overridepublic FileVisitResult visitFile(Path file,BasicFileAttributes attrs) throws IOException {//判断是否是一张照片if(file.toString().endsWith(".jpg")||file.toString().endsWith(".png")||file.toString().endsWith(".webp")){//备份图片File backImgFile=new File(backupPath+"\\"+file.toFile().getName());System.out.println(backImgFile);counter++;//方式1//创建输入流(用于读取图片)//创建输出流(用于写入图片)try ( BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file. toFile()));BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream(backImgFile));) {//边读边写byte[] buff = new byte[1024];int len = -1;while ((len = bis.read(buff)) != -1) {bos.write(buff,0, len);}}//方式2//Files. copy(file, backImgFile. toPath());}return super.visitFile(file, attrs);}});} catch (IOException e) {e.printStackTrace();}return counter;}
}
输出结果:
D:\test\img_back\tiantian.jpg
D:\test\img_back\tp.jpg
D:\test\img_back\微信图片_20230923231912.jpg
D:\test\img_back\微信图片_20230923231958.jpg
D:\test\img_back\微信图片_20230923232430.jpg
D:\test\img_back\微信图片_20230923232603.jpg
D:\test\img_back\微信图片_20240307183537.jpg
D:\test\img_back\微信图片_20240307183553.jpg
D:\test\img_back\微信图片_20240307183602.jpg
D:\test\img_back\微信图片_20240307183610.jpg
D:\test\img_back\微信图片_20240307183619.jpg
D:\test\img_back\微信图片_20240307183626.jpg
D:\test\img_back\2023.jpg
D:\test\img_back\king.jpg
D:\test\img_back\tiantian.jpg
D:\test\img_back\tp.jpg
D:\test\img_back\2023.jpg
D:\test\img_back\king.jpg
D:\test\img_back\tiantian.jpg
D:\test\img_back\tp.jpg
D:\test\img_back\微信图片_20230923231912.jpg
D:\test\img_back\微信图片_20230923231958.jpg
D:\test\img_back\微信图片_20230923232430.jpg
D:\test\img_back\微信图片_20230923232603.jpg
D:\test\img_back\微信图片_20240307183537.jpg
D:\test\img_back\微信图片_20240307183553.jpg
D:\test\img_back\微信图片_20240307183602.jpg
D:\test\img_back\微信图片_20240307183610.jpg
D:\test\img_back\微信图片_20240307183619.jpg
D:\test\img_back\微信图片_20240307183626.jpg
30