将一个文件夹下的某些或所有图像复制到另一个文件夹 java

本文介绍了一种使用Java程序批量从一个文件夹复制图像到另一个文件夹的方法,通过读取txt文件中的图像名称列表,实现图像的高效转移。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

某个txt文件保存着一个文件夹下的某些图像,我们需要把txt中包含的所有图像复制到另一个文件夹内进行保存

代码如下:

package csdn;

/**
 * 从存有图像名称的txt文件中复制某个文件夹下的指定名称的图像,将图像从一个文件夹复制到另一个文件夹
 * 原名复制
 */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class moveImage {
	public static void main(String[] args) throws IOException, InterruptedException {
		//存有原图像名称的文件,一行一个图像文件名(例如:pic.jpg)
		BufferedReader picSimi = new BufferedReader(new InputStreamReader(new FileInputStream(
				new File("C:\\Users\\DELL\\Desktop\\ImgName.txt")),"utf-8"));
		
		//将图像复制到此文件夹下
		String endpath = "C:\\Users\\DELL\\Desktop\\目标文件夹";
		File endpathdir = new File(endpath);
		if(!endpathdir.exists()) {
			endpathdir.mkdirs();
		}
		String picname = null;
		while((picname= picSimi.readLine())!=null) {
			String oripath = "C:\\Users\\DELL\\Desktop\\原图像\\"+picname;
			a(oripath,endpath);
		}
		picSimi.close();
	}

	//复制图片    
	public static void a(String path,String endpath) throws IOException {
		File imgpath = new File(path);
		if(!imgpath.exists())
			imgpath.mkdirs();
           
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(imgpath)) ;//创建输入的管道          
        byte[] buf = new byte[1024*20];//创建一个小数组
        int lenght = 0;
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(  //创建输出管道
                endpath+"\\"+path.substring(path.lastIndexOf("\\"),path.length()))); //图片会拷贝到这里
        System.out.println(endpath+"\\"+path.substring(path.lastIndexOf("\\"),path.length()));
        while ((lenght=bis.read(buf)) != -1) {
            bos.write(buf, 0, lenght);
        }
        bos.close();
        bis.close();
	}
 }

ImgName.txt的内容如下,一行保存一张图像名称:

原图像:

 

复制后的图像:

 

Java编程中,你可以使用`java.io.File`类以及`java.nio.file.Files`包下的功能来实现将一个文件夹下所有图片文件复制到一个指定位置。以下是一个简单的示例: ```java import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class CopyImages { public static void copyFolderToAnother(String sourceDirPath, String destinationDirPath) { try { // 获取源目录路径 Path source = Paths.get(sourceDirPath); // 检查源目录是否存在且是文件夹 if (!Files.isDirectory(source)) { throw new IllegalArgumentException("Source directory does not exist or is not a folder."); } // 创建目标目录(如果不存在) Files.createDirectories(Paths.get(destinationDirPath)); // 遍历源目录中的所有文件 for (Path entry : Files.newDirectoryStream(source)) { // 检查是否为图像文件,假设我们只关心.jpg和.png if (entry.toString().endsWith(".jpg") || entry.toString().endsWith(".png")) { // 获取文件名和目标路径 String fileName = entry.getFileName().toString(); Path targetFile = Paths.get(destinationDirPath, fileName); // 执行复制操作 Files.copy(entry, targetFile); System.out.println("Copied file " + fileName + " to " + targetFile); } } } catch (IOException e) { System.err.println("Error copying files: " + e.getMessage()); } } public static void main(String[] args) { String source = "/path/to/source/folder"; String destination = "/path/to/destination/folder"; copyFolderToAnother(source, destination); } } ``` 在这个示例中,你需要替换`source`和`destination`变量为你实际的文件夹路径。这个脚本会查找复制源目录下的所有`.jpg`和`.png`文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值