有梦就不苦,劳动最光荣。今天是五一劳动节,大家劳动节快乐哦!
趁着今天有时间,给女朋友用swing做了个小工具,用来解决她们电商系统导出来的图片数据跟图片库不匹配问题。所谓的不匹配,就是导出来的图片数据有52条,但是图片库只有49张图片,需要查出哪些数据没有对应的图片。
嗯哼。。。我一想,这不挺简单的吗?两行代码就可以OK的事情嘛。
File file = new File(filePath);
if (!file.exists()) {
//文件不存在
}
不过,转念一想,不能每次她需要查询的时候,就发一大堆文件给我,程序运行完之后再把结果告诉她,得让她自己查,自己动手,丰衣足食嘛。所以就想给她做个小工具。至于为啥要用swing来做呢?这种已经老到掉牙的东西,早就被人们遗忘在历史的长河里,而我呢,是因为方便简单快捷才用它,毕竟这种活没有啥收益的,能用就行啦,要啥自行车呢?哈哈。
小工具效果图:
图片库选择:
由于女朋友她们电商后台管理系统只能导出JPG格式的文件,本来想偷个懒,直接固定文件后缀名’.jpg’,然后就这样:
//查找图片是否存在
StringBuilder sb = new StringBuilder();
String[] datas = excelData.split("\n");
for (String data : datas) {
File file = new File(filePath + "/" + data + ".jpg");
if (!file.exists()) {
sb.append(data);
sb.append("\n");
}
}
resultText.setText(sb.toString());
这样确实是没啥问题的,但是这样非常有限制性的,如果是png的就无法使用了,或者是txt等其他文件格式。需要做得比较灵活,就要去掉文件后缀名,再进行查找。
//查找文件是否存在
StringBuilder sb = new StringBuilder();
String[] datas = excelData.split("\n");
try {
File files = new File(filePath);
String[] dist = files.list();
Set<String> distSet = new HashSet<>();
for (int i = 0; i < dist.length; i++) {
//去掉文件后缀
int index = dist[i].indexOf('.');
if (index != -1) {
String sFileName = dist[i].substring(0, index);
distSet.add(sFileName.toUpperCase());
}
}
for (String data : datas) {
if (!distSet.contains(data.toUpperCase())) {
sb.append(data);
sb.append("\n");
}
}
resultText.setText(sb.toString());
} catch(Exception var) {
resultText.setText(var.getMessage());
}
贴上全部代码:
public class Main {
private static void showConsole() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame jFrame = new JFrame("FindFilesConsole");
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 queryButton = new JButton("查询");
queryButton.setBounds(420, 350, 80, 25);
panel.add(queryButton);
//查询结果
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("");
}
});
//查询按钮点击事件
queryButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String filePath = filePathText.getText();
String excelData = excelDataText.getText();
if (filePath.length() == 0 || excelData.length() == 0) {
resultText.setText("查询出错");
return ;
}
//查找文件是否存在
StringBuilder sb = new StringBuilder();
String[] datas = excelData.split("\n");
try {
File files = new File(filePath);
String[] dist = files.list();
Set<String> distSet = new HashSet<>();
for (int i = 0; i < dist.length; i++) {
//去掉文件后缀
int index = dist[i].indexOf('.');
if (index != -1) {
String sFileName = dist[i].substring(0, index);
distSet.add(sFileName.toUpperCase());
}
}
for (String data : datas) {
if (!distSet.contains(data.toUpperCase())) {
sb.append(data);
sb.append("\n");
}
}
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放进去哦,最后新建一个批处理文件,命名为startFinder.bat。
startFinder.bat里面的内容是:
start /min jre/bin/java -jar FindFileTools.jar
- 做到这里,双击startFinder.bat文件基本就可以运行在没有JRE的电脑上了。如果想用起来更方便,可以创建快捷方式,配上一个图标。