快速批量複製文件

需求?

我女朋友提的。。。。。。她們電商後臺管理系統導出的身份證圖片庫,一批貨大概有個幾千張吧,但不是全部都用得上,需要根據導出的數據提取部分指定身份證的圖片。導出的數據有的還跟圖庫裏面的不一樣,因爲身份證的最後一位有的是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的電腦上了。如果想用起來更方便,可以創建快捷方式,配上一個圖標。
    在這裏插入圖片描述
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章