首页 技术 正文
技术 2022年11月9日
0 收藏 777 点赞 4,235 浏览 2350 个字

Tips

做一个终身学习的人!

日拱一卒,功不唐捐。

今天有个小小的需求,具体需求是这样的:

  1. 有两个文本文件,每个文件都有一些字符串文本;
  2. 比较第一个文件中,在第二个文件中,不存在的字符串文本;
  3. 把这些在第二个文件中不存在的文本,拼接成SQL的插入语句,写入到文件中。

具体第一个文件中的内容是这样的:

Java
Redis
Spring
Hibernate
Spring MVC
Dubbo
RabbitMq

第二个文件中的内容如下:

Java 9
ZooKepper
MongoDB
Spring Boot
Git
Spring MVC
Mybatis
Memcache
Dubbo
RabbitMq

其实,这个小功能很简单,逻辑也不复杂,就是一个可能大家比较常用的一个小工具。

根据我的思路,我主要使用了以下技术和工具:

  • Java 8 Lambda表达式;
  • Java 8 中提供的集合stream方法;
  • Google Guava 工具提供了集合比较的方法。

具体看代码:

package com.howtoprogram;import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;import com.google.common.collect.Sets;/**
* Created by 324779.
* 比较两个文件中文本内容的不同,把第一个文本中不同的部分挑选出来,
* 拼成对应的insert语句,
* 最后写入文件中。
*/
public class FilterDifferWords { public static void main(String[] args) throws IOException {
Path txtFile = Paths.get("/Users/i324779/Documents/test.txt");
List<String> txtContent = loadContentFromFile(txtFile); Path csvFile = Paths.get("/Users/i324779/Documents/test.csv");
List<String> csvContent = loadContentFromFile(csvFile); // List 转换为Set是为了文本内容去重
// 使用Google Guava工具中的Sets类
Set<String> differContent = Sets.difference(new HashSet<>(txtContent), new HashSet<>(csvContent)); List<String> insertSqls = spliceSql(differContent); writeToFile(insertSqls);
} // end method main /**
* @param filePath 文件路径
* @return 读取文件中内容到列表中。
* @throws IOException
*/
private static List<String> loadContentFromFile(Path filePath) throws IOException {
return Files.readAllLines(filePath).stream()
.map(String::trim) // 去掉文本的空格
.filter(line -> !("".equals(line))) // 过滤空行
.collect(Collectors.toList());
} // end method loadContentFromFile /**
* 拼装insert语句,使用MYSQL数据库的语法
* @param differContent 两个文本比较后,第一个文本有,而第二个文本没有的内容。
* @return 拼装好insert语句的列表
*/
private static List<String> spliceSql(Set<String> differContent) {
return differContent.stream()
.flatMap(line -> Stream.of(String.format(
"INSERT INTO test(search, replacement, level, expire, create_time) "
+ "VALUES ('%s', '*', 0, '2099-12-31 23:59:59', now());\r",
line)))
.collect(Collectors.toList());
} // end method spliceSql /**
* 把列表中的sql语句写入文件中
* @param insertSqls 拼装好insert语句的列表
* @throws IOException
*/
private static void writeToFile(List<String> insertSqls) throws IOException {
Path sqlFile = Paths.get("/Users/i324779/Documents/insertSql.txt");
Files.write(sqlFile, insertSqls);
} // end method writeToFile} // end class FilterDifferWords

执行结果为:

如果你有这样类似的需求,只需要把文件路径修改一下即可。

哈哈,程序员是一群为了偷懒而最勤奋想办法的一类人。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295