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

某个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的内容如下,一行保存一张图像名称:

原图像:

 

复制后的图像:

 

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值