快速批量复制文件

需求?

我女朋友提的。。。。。。她们电商后台管理系统导出的身份证图片库,一批货大概有个几千张吧,但不是全部都用得上,需要根据导出的数据提取部分指定身份证的图片。导出的数据有的还跟图库里面的不一样,因为身份证的最后一位有的是X,这边是大写的,那边是小写的,所以提取的时候还要忽略大小写的X。

还有导出的数据只有身份证号码,但是图库里面的图片名称是 身份证号码.s.jpg,注意到没有?这里是.s.jpg,而且每个口岸还不一样的后缀。

这个小程序是用Swing写的,非常方便简单快捷就做出来了。
程序截图:
在这里插入图片描述
实现思路也很简单:
首先获取图库下的文件名,将文件名.toUpperCase()大写形式存放到ArrayList里面,使用.toUpperCase()是为了遍历提取图片数据时两边同时.toUpperCase(),这样就可以忽略大小写了。根据ArrayList.indexOf(data)方法找到提取数据在图库里面的位置,从而复制此图片到新建的提取文件夹下。

复制文件

public class FileTools {
	 /**
	   * 通过JAVA NIO 非直接缓冲区拷贝文件
	   *
	   * @param sourcePath 源文件路径
	   * @param targetPath 目标文件路径
	   */
	  public static void copyFileByChannel(String sourcePath, String targetPath) {
	    FileChannel outChannel = null;
	    FileChannel inChannel = null;

	    FileInputStream fis = null;
	    FileOutputStream fos = null;

	    try {
	      fis = new FileInputStream(sourcePath);
	      fos = new FileOutputStream(targetPath);

	      //获取通道
	      inChannel = fis.getChannel();
	      outChannel = fos.getChannel();

	      //分配指定大小的缓冲区
	      ByteBuffer buf = ByteBuffer.allocate(1024);

	      while (inChannel.read(buf) != -1) {
	        //转换为读取数据模式
	        buf.flip();
	        //写入到磁盘
	        outChannel.write(buf);
	        //清空缓冲区
	        buf.clear();
	      }

	    } catch (Exception e) {
	      e.printStackTrace();
	    } finally {
	      //关闭流
	      try {
	        if (outChannel != null) {
	          outChannel.close();
	        }
	        if (inChannel != null) {
	          inChannel.close();
	        }
	        if (fis != null) {
	          fis.close();
	        }
	        if (fos != null) {
	          fos.close();
	        }
	      } catch (IOException e) {
	        e.printStackTrace();
	      }
	    }
	  }

}

当然不止上面一种方式复制文件,贴上地址:java实现文件拷贝的七种方式

具体实现

贴上小程序全部代码:

public class Main2 {
	
	private static void showConsole() {
		
		JFrame.setDefaultLookAndFeelDecorated(true);
		
		JFrame jFrame = new JFrame("RecoverFilesConsole");
		jFrame.setBounds(800, 400, 600, 600);
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		JPanel panel = new JPanel();  
		jFrame.add(panel);
		panel.setLayout(null);
		
		//选择文件路径
		JLabel filePathLabel = new JLabel("需要提取图片路径:");
		filePathLabel.setBounds(10, 20, 120, 30);
		panel.add(filePathLabel);
        JTextField filePathText = new JTextField(20);
        filePathText.setBounds(130, 20, 380, 30);
        panel.add(filePathText);
        JButton selectButton = new JButton("选择");
        selectButton.setBounds(520, 20, 60, 30);
        panel.add(selectButton);
        
        //excel表格数据
        JLabel excelDataLabel = new JLabel("excel表格数据:");
        excelDataLabel.setBounds(10, 80, 120, 30);
		panel.add(excelDataLabel);
        JTextArea excelDataText = new JTextArea();
        //添加滚动条
        JScrollPane excelDataJS = new JScrollPane(excelDataText);
        excelDataJS.setHorizontalScrollBarPolicy(
        		JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        excelDataJS.setVerticalScrollBarPolicy(
        		JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        excelDataJS.setBounds(130, 80, 450, 250);
        panel.add(excelDataJS);
        
        //重置
        JButton resetButton = new JButton("重置");
        resetButton.setBounds(300, 350, 80, 25);
        panel.add(resetButton);
        
        
        //提取
        JButton recoverButton = new JButton("提取");
        recoverButton.setBounds(420, 350, 80, 25);
        panel.add(recoverButton);
        
        //提取结果
        JLabel resultLabel = new JLabel("提取结果:");
        resultLabel.setBounds(10, 400, 120, 30);
		panel.add(resultLabel);
        JTextArea resultText = new JTextArea();
        //不可编辑
        resultText.setEditable(false);
        //添加滚动条
        JScrollPane resultJS = new JScrollPane(resultText);
        resultJS.setHorizontalScrollBarPolicy(
        		JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        resultJS.setVerticalScrollBarPolicy(
        		JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        resultJS.setBounds(130, 400, 450, 150);
        panel.add(resultJS);
        
		//显示窗口
		jFrame.setVisible(true);
		
		
		//选择按钮点击事件
		selectButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				//创建文件选择器
				JFileChooser fileChooser = new JFileChooser();
				//设置只显示目录
				fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				//显示对话框
				int ret = fileChooser.showOpenDialog(selectButton);
				//选择完成显示出来
				if (ret == JFileChooser.APPROVE_OPTION) {
					String filePath = fileChooser.getSelectedFile().getAbsolutePath();
					filePathText.setText(filePath);
				}
			}
		});
		
		//重置按钮点击事件
		resetButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				filePathText.setText("");
				excelDataText.setText("");
				resultText.setText("");
				
			}
		});
		
		//提取按钮点击事件
		recoverButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				//需要提取的文件夹
				String srcFilePath = filePathText.getText();
				//提取完之后存放的文件夹
				String distFilePath = filePathText.getText() + "/提取完成";
				File distFile = new File(distFilePath);
				if (!distFile.exists()) {
					distFile.mkdir();
				}
				//需要提取的Excel数据
				String excelData = excelDataText.getText();
				if (srcFilePath.length() == 0 || excelData.length() == 0) {
					resultText.setText("数据为空,无法提取!");
					return ;
				}
				String[] datas = excelData.split("\n");
				
				try {
					File files = new File(srcFilePath);
					ArrayList<String> srcFiles = new ArrayList<>();
					//获取提取文件夹下面的文件名
					String[] srcFilesName = files.list();
					//去掉文件后缀,用于判断文件存在的位置
					String[] srcFilesName_ = files.list();
					for (int i = 0; i < srcFilesName_.length; i++) {
						int index = srcFilesName_[i].indexOf('.');
						if (index != -1) {
							String sFileName = srcFilesName_[i].substring(0, index);
							srcFiles.add(sFileName.toUpperCase());
						}
					}
					for (String data : datas) {
						int index = srcFiles.indexOf(data.toUpperCase());
						if (index != -1) {
							//文件存在,复制此文件到提取完成存放的文件夹下面
						    String itFilePath = srcFilePath + "/" + srcFilesName[index];
							String copyFilePath = distFilePath + "/" + srcFilesName[index];
							FileTools.copyFileByChannel(itFilePath, copyFilePath);
						}
						
					}
					StringBuilder sb = new StringBuilder();
					sb.append("提取成功!\n");
					sb.append("提取文件路径:");
					sb.append(distFilePath);
					resultText.setText(sb.toString());
				} catch(Exception var) {
					resultText.setText(var.getMessage());
				}
				
			}
		});
	}
	
	public static void main(String[] args) {
		
		//显示控制台
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				
				showConsole();
				
			}
		});
	}

}

没有JRE如何使用?

经过几番测试,确定程序没有问题了,那就打包吧。
下面的步骤仅限于eclipse:

  1. 右键项目,选择Export,选择类型是Java/Runnable JAR file。
    在这里插入图片描述
  2. 配置好需要运行的Main类和jar包输出路径,最后点Finish。
    在这里插入图片描述
  3. 新建一个文件夹,把导出的jar包放进去,还要把JRE放进去哦,最后新建一个批处理文件,命名为startRecover.bat。
    在这里插入图片描述
    startRecover.bat里面的内容是:
start /min jre/bin/java -jar RecoverFileTools.jar
  1. 做到这里,双击startRecover.bat文件基本就可以运行在没有JRE的电脑上了。如果想用起来更方便,可以创建快捷方式,配上一个图标。
    在这里插入图片描述
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 批量复制文件到其他文件夹的方法是使用bat命令来进行操作。 首先,在电脑上任意位置建立一个新的记事本文件。然后,打开这个记事本文件并输入以下内容: xcopy 源文件夹路径 目标文件夹路径 /s/e/y 其中,将“源文件夹路径”替换成你要复制文件所在的文件夹路径,将“目标文件夹路径”替换成你要将文件复制到的目标文件夹路径。 /s表示复制子目录中的文件,/e表示包含子目录,/y表示覆盖目标文件夹中的同名文件。这些选项可以根据需要进行修改。 保存后将文件保存为.bat格式,比如“copyfiles.bat”。然后双击运行该文件,系统将自动执行批量复制文件到目标文件夹的操作。 需要注意的是,在复制文件之前,最好备份一下目标文件夹,以避免出现不必要的损失。同时,在文件复制完成后,可以通过对比源文件夹和目标文件夹中的文件,确认复制是否成功。 ### 回答2: 批量复制文件到其他文件夹,在Windows系统中,可以使用批处理文件(也叫bat文件)来实现。下面是具体操作步骤: 1. 打开记事本,创建一个新的文本文件。 2. 在文本文件中输入以下内容: xcopy "源文件夹路径\*.*" "目标文件夹路径\" /s /e 其中,“源文件夹路径”代表需要复制文件所在的文件夹路径,“目标文件夹路径”代表复制后要存放的文件夹路径。 3. 将“源文件夹路径”和“目标文件夹路径”换成实际的路径,并保存文件。注意,文件保存时需要将文件类型选择为“所有文件”,并将文件后缀名改为“.bat”。 4. 双击bat文件,即可开始批量复制文件到其他文件夹。 上述命令中的/s表示将复制子目录,/e表示复制所有文件,包括空文件夹。如果不需要复制子目录或空文件夹,可以去掉对应的参数。 另外,如果需要批量复制多个文件夹,可以将以上命令复制多次,并分别修改源文件夹路径和目标文件夹路径。这样一次性运行多个bat文件,即可快速将多个文件夹中的文件复制指定的目标文件夹中。 ### 回答3: 可以通过编写批处理文件批量复制文件到其他文件夹。 首先,需要打开记事本或其他文本编辑器。在新的文本文档中输入以下命令: xcopy "源文件夹路径\*.*" "目标文件夹路径" /s /e 需要将“源文件夹路径”替换为要复制文件夹的路径。“目标文件夹路径”则是要将文件复制到的文件夹路径。请确保路径是完整的,包括驱动器信息、斜杠和反斜杠。 通过在命令末尾添加/s /e选项,将同时复制文件夹和空文件夹。 接着,将文件另存为“*.bat”文件类型。可以命名为“copy.bat”或其他任意名称。请确保保存在易于访问的位置。 在运行前,请检查输入的路径是否正确。 双击文件即可运行。在命令提示符(cmd)窗口中,可以看到复制进度信息。 以上方法适用于 Windows 操作系统。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值