将一个文件夹下的某些或所有图像复制到另一个文件夹 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的内容如下,一行保存一张图像名称:

原图像:

 

复制后的图像:

 

发布了11 篇原创文章 · 获赞 6 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/m0_37762912/article/details/84330672