需求?
我女朋友提的。。。。。。她们电商后台管理系统导出的身份证图片库,一批货大概有个几千张吧,但不是全部都用得上,需要根据导出的数据提取部分指定身份证的图片。导出的数据有的还跟图库里面的不一样,因为身份证的最后一位有的是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:
- 右键项目,选择Export,选择类型是Java/Runnable JAR file。
- 配置好需要运行的Main类和jar包输出路径,最后点Finish。
- 新建一个文件夹,把导出的jar包放进去,还要把JRE放进去哦,最后新建一个批处理文件,命名为startRecover.bat。
startRecover.bat里面的内容是:
start /min jre/bin/java -jar RecoverFileTools.jar
- 做到这里,双击startRecover.bat文件基本就可以运行在没有JRE的电脑上了。如果想用起来更方便,可以创建快捷方式,配上一个图标。