简单实现文件遍历、比较、复制功能

前言:

       大四实习,第一个月忙着给公司处理杂活,终于这两天把标准文档翻译完了,用户说明书什么的也都写好了,领导派下来了一些小杂活,当作练练手吧。


正式内容:

        这个小程序主要是实现了如下功能:

        注:data下存储静态常量,方便修改程序。

package tsc.data;

public class Data {

	//源bin文件所在绝对路径
	public static final String SOURCE = "D:/ceshi/Source/source.bin";

	//需要遍历的路径
	public static final String PATH = "D:/ceshi/Test";
	
	//文档要对比的字节数
	public static final int BYTE = 64;
}

        1.在某个文件夹下进行遍历,去寻找后缀名为.bin的文件


package tsc.function;

import java.io.File;
import tsc.data.Data;

/**
 * 遍历功能
 * @author qiuyue
 *
 */

public class TraverseFolder {
	
	public static void traverseFolder(String path) {
		File file = new File(path);
		if(file.exists()) {
			//如果路径存在,则返回此路径目录下所有文件和目录的绝对路径,返回的是File数组
			File[] files = file.listFiles();
			if(files.length == 0) {
				return;
			} else {
				for(File file2 : files) {
					if(file2.isDirectory()) {
						//如果遍历中遇到了文件夹,则对此文件夹再进行遍历
						traverseFolder(file2.getAbsolutePath());
					} else {
							if(file2.getName().endsWith("bin")) {
								//如果遇到了.bin文件,则调用CompareFile进行比较
								CompareFile.compareFile(file2.getAbsolutePath(),Data.SOURCE); 
							}
					}
				}
			}
		} else {
			return;
		}
	}
}

        2.如果找到了.bin文件,则将其与另外一个.bin文件进行比较(IO流)

此处先比较了两文件前64个字节,如果一样证明为同一类文件,可以复制,下面再比较长度,长度不同再比较内容,只要有一项不同就进行复制。

package tsc.function;

import java.io.FileInputStream;
import java.io.IOException;
import tsc.data.Data;

/**
 * 文件比较功能
 * @author qiuyue
 *
 */

public class CompareFile {
	
	public static boolean compareFile(String file,String source) {
		
		FileInputStream f1 = null;
		FileInputStream f2 = null;
		
		try {
			f1 = new FileInputStream(file);
			f2 = new FileInputStream(source);
			byte[] data1 = new byte[Data.BYTE];
			byte[] data2 = new byte[Data.BYTE];
			int len1 = f1.available();
			int len2 = f2.available();
			f1.read(data1);
			f2.read(data2);
			for(int i=0;i<Data.BYTE;i++) {
				if(data1[i] != data2[i]) {
					System.out.println("两个文件不是同一类文件");
					return false;
				}
			}
			System.out.println("两个文件是同一类文件");
			if(len1 == len2){
				byte[] data3 = new byte[len1];
				byte[] data4 = new byte[len2];
				f1.read(data3);
				f2.read(data4);
				for(int i=0;i<len1;i++){
					if(data3[i] != data4[i]){
						System.out.println("两个文件长度相同,内容不相同,开始复制");
						CopyFile.copyFile(file,source);
						return true;
					}
				}
				System.out.println("两个文件完全相同,不再复制");
				return false;
			}
			System.out.println("两个文件长度不相同,开始复制");
			CopyFile.copyFile(file,source);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(f1 != null) {
				try {
					f1.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(f2 != null) {
				try {
					f2.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return false;
	}
	
}

        3.比较完成,符合的就将其复制到SOURCE

package tsc.function;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 文件复制功能
 * @author qiuyue
 *
 */

public class CopyFile {
	
	public static boolean copyFile(String file,String source) {
		int byteread = 0;
		InputStream in = null;
		OutputStream out = null;
		
		try {
			in = new FileInputStream(file);
			out = new FileOutputStream(source);
			byte[] buffer = new byte[2048];
			while((byteread = in.read(buffer)) != -1) {
				out.write(buffer, 0, byteread);
			}
			System.out.println("复制成功");
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally {
			if(out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		
	}
}

附:1.定时器

package tsc.function;

import java.util.TimerTask;
import tsc.data.Data;

/**
 * 定时器
 * @author qiuyue
 *
 */

public class MyTask {
	
	public static class myTask extends TimerTask{
		public void run() {
			TraverseFolder.traverseFolder(Data.PATH);
		}
	}
}
       2.前端小窗口

package tsc.ui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;

import javax.swing.*;

import tsc.function.MyTask.myTask;

public class MyFrame extends JFrame{

	private static final long serialVersionUID = 371204056893587903L;

	public MyFrame() {
		this.setTitle("bin文件比较程序");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(300, 150);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setLayout(new GridLayout(1, 2));
		JButton start = new JButton("开始");
		JButton exit = new JButton("退出");
		start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Timer timer = new Timer();
				myTask mytask = new myTask();
				timer.scheduleAtFixedRate(mytask, 0, 5000);
				setExtendedState(JFrame.ICONIFIED);
			}
		});
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		getContentPane().add(start);
		getContentPane().add(exit);
	}
}
        3.主程序

package tsc.main;

import tsc.ui.MyFrame;

public class Main {

	public static void main(String[] args) {
		new MyFrame().setVisible(true);
	}

}

        4.引入的jar包


就这么个简单的小程序,给新手一个借鉴。(虽然我也是超级萌新)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现这个功能,可以使用Python的os模块来遍历文件夹,并使用shutil模块来复制图片。 首先,我们需要定义一个函数,用来遍历文件夹并复制图片。我们可以使用os模块的walk方法来遍历文件的所有文件和子文件夹,然后使用os.path.splitext方法来判断文件的扩展名是否为图片格式(如.jpg, .png, .gif等)。 一旦找到了符合条件的图片文件,我们就可以使用shutil模块的copy方法来复制这些图片到我们指定的目标文件。 下面是一个简单的示例代码: ```python import os import shutil def copy_images(source_folder, destination_folder): for root, dirs, files in os.walk(source_folder): for file in files: if os.path.splitext(file)[1].lower() in ('.jpg', '.png', '.gif'): source_path = os.path.join(root, file) destination_path = os.path.join(destination_folder, file) shutil.copyfile(source_path, destination_path) source_folder = 'path/to/source/folder' destination_folder = 'path/to/destination/folder' copy_images(source_folder, destination_folder) ``` 在这个示例代码,我们首先定义了一个copy_images函数,接收源文件夹和目标文件夹作为参数。然后,我们使用os.walk方法遍历文件的所有文件和子文件夹。对于每一个文件,我们使用os.path.splitext方法来获取文件扩展名,并判断是否为图片格式。如果是图片格式的文件,我们就使用shutil.copyfile方法来复制文件到指定的目标文件。 这样,通过这个简单的示例代码,我们就可以实现Python遍历文件夹按要求复制出图片的功能

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值